Sum of two numbers and the numbers between them












2














Ask user to enter a number. Then ask the user to enter a number bigger than the first number. Calculate a value that is the sum of all the numbers from the first number to the second number including the end points. For example, if the user enters 5 and 10 then calculate the total of 5 + 6 + 7 + 8 + 9 + 10. Display the results. MUST USE A WHILE LOOP



This is what I have so far. I can't seem to get the sum to be correct. If I enter the numbers from the example above, I get 49 instead of 45. I understand where I went wrong and why it is 49 based on my code but can't figure out how to make it correct.






// declare constants
const INITIAL_VALUE = 0;
const COUNTER_VALUE = 1;

// declare variables
var number1;
var number2;
var sum;
var counter;
var difference;
var middlePoint;
var middlePointSum;

// assigning values
sum = INITIAL_VALUE;
difference = INITIAL_VALUE;
counter = COUNTER_VALUE;
middlePoint = COUNTER_VALUE;
middlePointSum = INITIAL_VALUE;

// prompt user to enter two numbers
number1 = prompt("Enter first number: ");
number2 = prompt("Enter a number bigger than first number: ");

// convert user input into numbers
number1 = Number(number1);
number2 = Number(number2);

// display number1
document.write(number1);
difference = number2 - number1;
middlePoint += number1;

while (counter < difference) {
document.write(" + " + middlePoint);
middlePoint = middlePoint + 1;
counter++;
middlePointSum += middlePoint;
}

// calculate the sum
sum = number1 + middlePointSum + number2;

// display number1, middle points, number2, and sum
document.write(" + " + number2 + " = " + sum);












share|improve this question





























    2














    Ask user to enter a number. Then ask the user to enter a number bigger than the first number. Calculate a value that is the sum of all the numbers from the first number to the second number including the end points. For example, if the user enters 5 and 10 then calculate the total of 5 + 6 + 7 + 8 + 9 + 10. Display the results. MUST USE A WHILE LOOP



    This is what I have so far. I can't seem to get the sum to be correct. If I enter the numbers from the example above, I get 49 instead of 45. I understand where I went wrong and why it is 49 based on my code but can't figure out how to make it correct.






    // declare constants
    const INITIAL_VALUE = 0;
    const COUNTER_VALUE = 1;

    // declare variables
    var number1;
    var number2;
    var sum;
    var counter;
    var difference;
    var middlePoint;
    var middlePointSum;

    // assigning values
    sum = INITIAL_VALUE;
    difference = INITIAL_VALUE;
    counter = COUNTER_VALUE;
    middlePoint = COUNTER_VALUE;
    middlePointSum = INITIAL_VALUE;

    // prompt user to enter two numbers
    number1 = prompt("Enter first number: ");
    number2 = prompt("Enter a number bigger than first number: ");

    // convert user input into numbers
    number1 = Number(number1);
    number2 = Number(number2);

    // display number1
    document.write(number1);
    difference = number2 - number1;
    middlePoint += number1;

    while (counter < difference) {
    document.write(" + " + middlePoint);
    middlePoint = middlePoint + 1;
    counter++;
    middlePointSum += middlePoint;
    }

    // calculate the sum
    sum = number1 + middlePointSum + number2;

    // display number1, middle points, number2, and sum
    document.write(" + " + number2 + " = " + sum);












    share|improve this question



























      2












      2








      2







      Ask user to enter a number. Then ask the user to enter a number bigger than the first number. Calculate a value that is the sum of all the numbers from the first number to the second number including the end points. For example, if the user enters 5 and 10 then calculate the total of 5 + 6 + 7 + 8 + 9 + 10. Display the results. MUST USE A WHILE LOOP



      This is what I have so far. I can't seem to get the sum to be correct. If I enter the numbers from the example above, I get 49 instead of 45. I understand where I went wrong and why it is 49 based on my code but can't figure out how to make it correct.






      // declare constants
      const INITIAL_VALUE = 0;
      const COUNTER_VALUE = 1;

      // declare variables
      var number1;
      var number2;
      var sum;
      var counter;
      var difference;
      var middlePoint;
      var middlePointSum;

      // assigning values
      sum = INITIAL_VALUE;
      difference = INITIAL_VALUE;
      counter = COUNTER_VALUE;
      middlePoint = COUNTER_VALUE;
      middlePointSum = INITIAL_VALUE;

      // prompt user to enter two numbers
      number1 = prompt("Enter first number: ");
      number2 = prompt("Enter a number bigger than first number: ");

      // convert user input into numbers
      number1 = Number(number1);
      number2 = Number(number2);

      // display number1
      document.write(number1);
      difference = number2 - number1;
      middlePoint += number1;

      while (counter < difference) {
      document.write(" + " + middlePoint);
      middlePoint = middlePoint + 1;
      counter++;
      middlePointSum += middlePoint;
      }

      // calculate the sum
      sum = number1 + middlePointSum + number2;

      // display number1, middle points, number2, and sum
      document.write(" + " + number2 + " = " + sum);












      share|improve this question















      Ask user to enter a number. Then ask the user to enter a number bigger than the first number. Calculate a value that is the sum of all the numbers from the first number to the second number including the end points. For example, if the user enters 5 and 10 then calculate the total of 5 + 6 + 7 + 8 + 9 + 10. Display the results. MUST USE A WHILE LOOP



      This is what I have so far. I can't seem to get the sum to be correct. If I enter the numbers from the example above, I get 49 instead of 45. I understand where I went wrong and why it is 49 based on my code but can't figure out how to make it correct.






      // declare constants
      const INITIAL_VALUE = 0;
      const COUNTER_VALUE = 1;

      // declare variables
      var number1;
      var number2;
      var sum;
      var counter;
      var difference;
      var middlePoint;
      var middlePointSum;

      // assigning values
      sum = INITIAL_VALUE;
      difference = INITIAL_VALUE;
      counter = COUNTER_VALUE;
      middlePoint = COUNTER_VALUE;
      middlePointSum = INITIAL_VALUE;

      // prompt user to enter two numbers
      number1 = prompt("Enter first number: ");
      number2 = prompt("Enter a number bigger than first number: ");

      // convert user input into numbers
      number1 = Number(number1);
      number2 = Number(number2);

      // display number1
      document.write(number1);
      difference = number2 - number1;
      middlePoint += number1;

      while (counter < difference) {
      document.write(" + " + middlePoint);
      middlePoint = middlePoint + 1;
      counter++;
      middlePointSum += middlePoint;
      }

      // calculate the sum
      sum = number1 + middlePointSum + number2;

      // display number1, middle points, number2, and sum
      document.write(" + " + number2 + " = " + sum);








      // declare constants
      const INITIAL_VALUE = 0;
      const COUNTER_VALUE = 1;

      // declare variables
      var number1;
      var number2;
      var sum;
      var counter;
      var difference;
      var middlePoint;
      var middlePointSum;

      // assigning values
      sum = INITIAL_VALUE;
      difference = INITIAL_VALUE;
      counter = COUNTER_VALUE;
      middlePoint = COUNTER_VALUE;
      middlePointSum = INITIAL_VALUE;

      // prompt user to enter two numbers
      number1 = prompt("Enter first number: ");
      number2 = prompt("Enter a number bigger than first number: ");

      // convert user input into numbers
      number1 = Number(number1);
      number2 = Number(number2);

      // display number1
      document.write(number1);
      difference = number2 - number1;
      middlePoint += number1;

      while (counter < difference) {
      document.write(" + " + middlePoint);
      middlePoint = middlePoint + 1;
      counter++;
      middlePointSum += middlePoint;
      }

      // calculate the sum
      sum = number1 + middlePointSum + number2;

      // display number1, middle points, number2, and sum
      document.write(" + " + number2 + " = " + sum);





      // declare constants
      const INITIAL_VALUE = 0;
      const COUNTER_VALUE = 1;

      // declare variables
      var number1;
      var number2;
      var sum;
      var counter;
      var difference;
      var middlePoint;
      var middlePointSum;

      // assigning values
      sum = INITIAL_VALUE;
      difference = INITIAL_VALUE;
      counter = COUNTER_VALUE;
      middlePoint = COUNTER_VALUE;
      middlePointSum = INITIAL_VALUE;

      // prompt user to enter two numbers
      number1 = prompt("Enter first number: ");
      number2 = prompt("Enter a number bigger than first number: ");

      // convert user input into numbers
      number1 = Number(number1);
      number2 = Number(number2);

      // display number1
      document.write(number1);
      difference = number2 - number1;
      middlePoint += number1;

      while (counter < difference) {
      document.write(" + " + middlePoint);
      middlePoint = middlePoint + 1;
      counter++;
      middlePointSum += middlePoint;
      }

      // calculate the sum
      sum = number1 + middlePointSum + number2;

      // display number1, middle points, number2, and sum
      document.write(" + " + number2 + " = " + sum);






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 10:26









      Eugene Mihaylin

      9601424




      9601424










      asked Nov 12 at 8:44









      M.P.T.

      477




      477
























          7 Answers
          7






          active

          oldest

          votes


















          1

















          // prompt user to enter two numbers
          var number1 = prompt("Enter first number: ");
          var number2 = prompt("Enter a number bigger than first number: ");

          // convert user input into numbers
          var number1 = Number(number1);
          var number2 = Number(number2);

          var start_point= number1;
          var sum=0;

          // display number1
          document.write(start_point);
          sum += start_point;
          start_point++;

          while (start_point <=number2) {
          document.write(" + " + start_point);
          sum += start_point;
          start_point++;
          }


          // display sum
          document.write(" = " + sum);








          share|improve this answer





























            1














            Welcome to StackOverflow!



            The cause of error is at this part of the code



            while (counter < difference) {
            document.write(" + " + middlePoint);
            middlePoint = middlePoint + 1; // you are already increasing the middlePoint number
            counter++;
            middlePointSum += middlePoint;
            }


            Therefore, the sum is always over by the number of counter - 1. What you should have done, is execute middlePointSum += middlePoint; first before increasing the value of middlePoint and counter.



            while (counter < difference) {
            document.write(" + " + middlePoint);
            middlePointSum += middlePoint;
            middlePoint = middlePoint + 1;
            counter++;
            }





            share|improve this answer





























              1














              This code should work for you. Variabel sum should contain your desired sum.



              var sum = 0;
              var firstNumber = 0;
              var secondNUmber = 0;
              var firstNumberStr = '';
              var secondNUmberStr = '';

              // prompt user to enter two numbers
              firstNumberStr = prompt("Enter first number: ");

              while(isNaN(firstNumberStr)){
              firstNumberStr = prompt("Enter Correct first number: ");
              }

              secondNUmberStr = prompt("Enter a number bigger than first number: ");

              while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
              secondNUmberStr = prompt("Enter a number bigger than first number: ");
              }

              firstNumber = parseInt(firstNumberStr);
              secondNUmber = parseInt(secondNUmberStr);

              while(firstNumber <= secondNUmber){
              sum += firstNumber;
              firstNumber++
              }





              share|improve this answer





























                1














                You did a very small mistake in the code . The bug is in while loop.



                 while (counter < difference) {
                document.write(" + " + middlePoint);
                middlePoint = middlePoint + 1;
                counter++;
                middlePointSum += middlePoint;
                }


                you are incrementing the middlepoint before middlePointSum is Calculated. It should be incremented after calculating the middlePointSum.



                while (counter < difference) {
                document.write(" + " + middlePoint);
                counter++;
                middlePointSum += middlePoint;
                middlePoint = middlePoint + 1;
                }





                share|improve this answer





























                  1














                  Your middlePointSum is starting from 1 intead of 0 so you got: 5 + (7 + 8 + 9 + 10) + 10 to fix your code set middlePointSum to INITIAL_VALUE



                  For simplification just ignore the difference between the numbers, increment number1 until you reach number 2






                  var number1 = prompt("Enter first number: ");
                  var number2 = prompt("Enter a number bigger than first number: ");
                  var incNumber;
                  var strResult = ;

                  number1 = incNumber = Number(number1);
                  // use incNumber if you need to preserve number1 value or just use number 1 value instead
                  number2 = Number(number2);

                  var sum = 0;
                  while(incNumber <= number2) {
                  strResult.push(incNumber);
                  sum += incNumber++;
                  // this means add incNumber to sum then increment it by 1
                  }

                  document.write(`${strResult.join(' + ')} = ${sum}`);








                  share|improve this answer





























                    -1














                    One problem is that your middlePointSum does not take into account the first number after the number1, for example, the 6 in your example. Another problem is that your counter < difference test is inaccurate - you would have to iterate until counter + 1 < difference:






                    // declare constants
                    const INITIAL_VALUE = 0;
                    const COUNTER_VALUE = 1;

                    // declare variables
                    var number1;
                    var number2;
                    var sum;
                    var counter;
                    var difference;
                    var middlePoint;
                    var middlePointSum;

                    // assigning values
                    sum = INITIAL_VALUE;
                    difference = INITIAL_VALUE;
                    counter = COUNTER_VALUE;
                    middlePoint = COUNTER_VALUE;
                    middlePointSum = INITIAL_VALUE;

                    // prompt user to enter two numbers
                    number1 = prompt("Enter first number: ");
                    number2 = prompt("Enter a number bigger than first number: ");

                    // convert user input into numbers
                    number1 = Number(number1);
                    number2 = Number(number2);

                    // display number1
                    document.write(number1);

                    difference = number2 - number1;
                    middlePoint += number1;

                    middlePointSum = middlePoint;
                    while (counter + 1 < difference) {
                    document.write(" + " + middlePoint);
                    middlePoint = middlePoint + 1;
                    counter++;
                    console.log(middlePointSum);
                    middlePointSum += middlePoint;
                    }

                    // calculate the sum
                    sum = number1 + middlePointSum + number2;

                    // display number1, middle points, number2, and sum
                    document.write(" + " + number2 + " = " + sum);





                    But this seems far, far more complicated than it needs to be - why not simply iterate from number1 to number2, adding to sum along the way?






                    const number1 = Number(prompt("Enter first number: "));
                    const number2 = Number(prompt("Enter a number bigger than first number: "));
                    document.write(number1);
                    let sum = number1;
                    for (let i = number1 + 1; i <= number2; i++) {
                    sum += i;
                    document.write(' + ' + i);
                    }
                    document.write(' = ' + sum);





                    Or, create an array of the numbers, join by plus signs, and calculate the sum with reduce:






                    const number1 = Number(prompt("Enter first number: "));
                    const number2 = Number(prompt("Enter a number bigger than first number: "));

                    const nums = Array.from(
                    { length: number2 - number1 + 1 },
                    (_, i) => i + number1
                    );
                    const sum = nums.reduce((a, b) => a + b);
                    document.write(nums.join(' + ') + ' = ' + sum);








                    share|improve this answer





























                      -1

















                      var num1 = Number(prompt("Enter a number"));
                      var num2 = Number(prompt("Enter a larger number"));


                      document.write(num1 + "+" + num2 + "=" + (num1+num2));



                      // now lets find the sum of numbers between these two numbers.
                      document.write("<br>"); // new line

                      var counter = num1; // we will start countring from the smaller number
                      var sum = 0;

                      //keep looping until we reach the bigger number (num2)
                      while(counter<=num2) {
                      sum += counter;
                      // print the current number
                      document.write(counter);
                      //print a plus sign
                      if(counter<num2) {
                      // only put a (plus sign) if we are not yet reached num2
                      document.write("+");
                      }

                      //increment to control the loop
                      counter ++;

                      }


                      //finally put the result
                      document.write("=" + sum);





                      You are setting counter to the constant COUNTER_VALUE which is 1.



                      Then, in your program, you are entering 5 and 6 as input. The difference is 6-5 which is 1.



                      Your while loop has a condition to only execute when counter<difference . In your case, counter=1 and difference=1. This means that they are equal, and thus, the loop will not execute.



                      I hope this sheds light on what needs to be done from your side to fix this bug.






                      share|improve this answer























                        Your Answer






                        StackExchange.ifUsing("editor", function () {
                        StackExchange.using("externalEditor", function () {
                        StackExchange.using("snippets", function () {
                        StackExchange.snippets.init();
                        });
                        });
                        }, "code-snippets");

                        StackExchange.ready(function() {
                        var channelOptions = {
                        tags: "".split(" "),
                        id: "1"
                        };
                        initTagRenderer("".split(" "), "".split(" "), channelOptions);

                        StackExchange.using("externalEditor", function() {
                        // Have to fire editor after snippets, if snippets enabled
                        if (StackExchange.settings.snippets.snippetsEnabled) {
                        StackExchange.using("snippets", function() {
                        createEditor();
                        });
                        }
                        else {
                        createEditor();
                        }
                        });

                        function createEditor() {
                        StackExchange.prepareEditor({
                        heartbeatType: 'answer',
                        autoActivateHeartbeat: false,
                        convertImagesToLinks: true,
                        noModals: true,
                        showLowRepImageUploadWarning: true,
                        reputationToPostImages: 10,
                        bindNavPrevention: true,
                        postfix: "",
                        imageUploader: {
                        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                        allowUrls: true
                        },
                        onDemand: true,
                        discardSelector: ".discard-answer"
                        ,immediatelyShowMarkdownHelp:true
                        });


                        }
                        });














                        draft saved

                        draft discarded


















                        StackExchange.ready(
                        function () {
                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258500%2fsum-of-two-numbers-and-the-numbers-between-them%23new-answer', 'question_page');
                        }
                        );

                        Post as a guest















                        Required, but never shown

























                        7 Answers
                        7






                        active

                        oldest

                        votes








                        7 Answers
                        7






                        active

                        oldest

                        votes









                        active

                        oldest

                        votes






                        active

                        oldest

                        votes









                        1

















                        // prompt user to enter two numbers
                        var number1 = prompt("Enter first number: ");
                        var number2 = prompt("Enter a number bigger than first number: ");

                        // convert user input into numbers
                        var number1 = Number(number1);
                        var number2 = Number(number2);

                        var start_point= number1;
                        var sum=0;

                        // display number1
                        document.write(start_point);
                        sum += start_point;
                        start_point++;

                        while (start_point <=number2) {
                        document.write(" + " + start_point);
                        sum += start_point;
                        start_point++;
                        }


                        // display sum
                        document.write(" = " + sum);








                        share|improve this answer


























                          1

















                          // prompt user to enter two numbers
                          var number1 = prompt("Enter first number: ");
                          var number2 = prompt("Enter a number bigger than first number: ");

                          // convert user input into numbers
                          var number1 = Number(number1);
                          var number2 = Number(number2);

                          var start_point= number1;
                          var sum=0;

                          // display number1
                          document.write(start_point);
                          sum += start_point;
                          start_point++;

                          while (start_point <=number2) {
                          document.write(" + " + start_point);
                          sum += start_point;
                          start_point++;
                          }


                          // display sum
                          document.write(" = " + sum);








                          share|improve this answer
























                            1












                            1








                            1









                            // prompt user to enter two numbers
                            var number1 = prompt("Enter first number: ");
                            var number2 = prompt("Enter a number bigger than first number: ");

                            // convert user input into numbers
                            var number1 = Number(number1);
                            var number2 = Number(number2);

                            var start_point= number1;
                            var sum=0;

                            // display number1
                            document.write(start_point);
                            sum += start_point;
                            start_point++;

                            while (start_point <=number2) {
                            document.write(" + " + start_point);
                            sum += start_point;
                            start_point++;
                            }


                            // display sum
                            document.write(" = " + sum);








                            share|improve this answer















                            // prompt user to enter two numbers
                            var number1 = prompt("Enter first number: ");
                            var number2 = prompt("Enter a number bigger than first number: ");

                            // convert user input into numbers
                            var number1 = Number(number1);
                            var number2 = Number(number2);

                            var start_point= number1;
                            var sum=0;

                            // display number1
                            document.write(start_point);
                            sum += start_point;
                            start_point++;

                            while (start_point <=number2) {
                            document.write(" + " + start_point);
                            sum += start_point;
                            start_point++;
                            }


                            // display sum
                            document.write(" = " + sum);








                            // prompt user to enter two numbers
                            var number1 = prompt("Enter first number: ");
                            var number2 = prompt("Enter a number bigger than first number: ");

                            // convert user input into numbers
                            var number1 = Number(number1);
                            var number2 = Number(number2);

                            var start_point= number1;
                            var sum=0;

                            // display number1
                            document.write(start_point);
                            sum += start_point;
                            start_point++;

                            while (start_point <=number2) {
                            document.write(" + " + start_point);
                            sum += start_point;
                            start_point++;
                            }


                            // display sum
                            document.write(" = " + sum);





                            // prompt user to enter two numbers
                            var number1 = prompt("Enter first number: ");
                            var number2 = prompt("Enter a number bigger than first number: ");

                            // convert user input into numbers
                            var number1 = Number(number1);
                            var number2 = Number(number2);

                            var start_point= number1;
                            var sum=0;

                            // display number1
                            document.write(start_point);
                            sum += start_point;
                            start_point++;

                            while (start_point <=number2) {
                            document.write(" + " + start_point);
                            sum += start_point;
                            start_point++;
                            }


                            // display sum
                            document.write(" = " + sum);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 9:01









                            Dilip Belgumpi

                            633313




                            633313

























                                1














                                Welcome to StackOverflow!



                                The cause of error is at this part of the code



                                while (counter < difference) {
                                document.write(" + " + middlePoint);
                                middlePoint = middlePoint + 1; // you are already increasing the middlePoint number
                                counter++;
                                middlePointSum += middlePoint;
                                }


                                Therefore, the sum is always over by the number of counter - 1. What you should have done, is execute middlePointSum += middlePoint; first before increasing the value of middlePoint and counter.



                                while (counter < difference) {
                                document.write(" + " + middlePoint);
                                middlePointSum += middlePoint;
                                middlePoint = middlePoint + 1;
                                counter++;
                                }





                                share|improve this answer


























                                  1














                                  Welcome to StackOverflow!



                                  The cause of error is at this part of the code



                                  while (counter < difference) {
                                  document.write(" + " + middlePoint);
                                  middlePoint = middlePoint + 1; // you are already increasing the middlePoint number
                                  counter++;
                                  middlePointSum += middlePoint;
                                  }


                                  Therefore, the sum is always over by the number of counter - 1. What you should have done, is execute middlePointSum += middlePoint; first before increasing the value of middlePoint and counter.



                                  while (counter < difference) {
                                  document.write(" + " + middlePoint);
                                  middlePointSum += middlePoint;
                                  middlePoint = middlePoint + 1;
                                  counter++;
                                  }





                                  share|improve this answer
























                                    1












                                    1








                                    1






                                    Welcome to StackOverflow!



                                    The cause of error is at this part of the code



                                    while (counter < difference) {
                                    document.write(" + " + middlePoint);
                                    middlePoint = middlePoint + 1; // you are already increasing the middlePoint number
                                    counter++;
                                    middlePointSum += middlePoint;
                                    }


                                    Therefore, the sum is always over by the number of counter - 1. What you should have done, is execute middlePointSum += middlePoint; first before increasing the value of middlePoint and counter.



                                    while (counter < difference) {
                                    document.write(" + " + middlePoint);
                                    middlePointSum += middlePoint;
                                    middlePoint = middlePoint + 1;
                                    counter++;
                                    }





                                    share|improve this answer












                                    Welcome to StackOverflow!



                                    The cause of error is at this part of the code



                                    while (counter < difference) {
                                    document.write(" + " + middlePoint);
                                    middlePoint = middlePoint + 1; // you are already increasing the middlePoint number
                                    counter++;
                                    middlePointSum += middlePoint;
                                    }


                                    Therefore, the sum is always over by the number of counter - 1. What you should have done, is execute middlePointSum += middlePoint; first before increasing the value of middlePoint and counter.



                                    while (counter < difference) {
                                    document.write(" + " + middlePoint);
                                    middlePointSum += middlePoint;
                                    middlePoint = middlePoint + 1;
                                    counter++;
                                    }






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 12 at 8:53









                                    Andreas

                                    1,7961618




                                    1,7961618























                                        1














                                        This code should work for you. Variabel sum should contain your desired sum.



                                        var sum = 0;
                                        var firstNumber = 0;
                                        var secondNUmber = 0;
                                        var firstNumberStr = '';
                                        var secondNUmberStr = '';

                                        // prompt user to enter two numbers
                                        firstNumberStr = prompt("Enter first number: ");

                                        while(isNaN(firstNumberStr)){
                                        firstNumberStr = prompt("Enter Correct first number: ");
                                        }

                                        secondNUmberStr = prompt("Enter a number bigger than first number: ");

                                        while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
                                        secondNUmberStr = prompt("Enter a number bigger than first number: ");
                                        }

                                        firstNumber = parseInt(firstNumberStr);
                                        secondNUmber = parseInt(secondNUmberStr);

                                        while(firstNumber <= secondNUmber){
                                        sum += firstNumber;
                                        firstNumber++
                                        }





                                        share|improve this answer


























                                          1














                                          This code should work for you. Variabel sum should contain your desired sum.



                                          var sum = 0;
                                          var firstNumber = 0;
                                          var secondNUmber = 0;
                                          var firstNumberStr = '';
                                          var secondNUmberStr = '';

                                          // prompt user to enter two numbers
                                          firstNumberStr = prompt("Enter first number: ");

                                          while(isNaN(firstNumberStr)){
                                          firstNumberStr = prompt("Enter Correct first number: ");
                                          }

                                          secondNUmberStr = prompt("Enter a number bigger than first number: ");

                                          while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
                                          secondNUmberStr = prompt("Enter a number bigger than first number: ");
                                          }

                                          firstNumber = parseInt(firstNumberStr);
                                          secondNUmber = parseInt(secondNUmberStr);

                                          while(firstNumber <= secondNUmber){
                                          sum += firstNumber;
                                          firstNumber++
                                          }





                                          share|improve this answer
























                                            1












                                            1








                                            1






                                            This code should work for you. Variabel sum should contain your desired sum.



                                            var sum = 0;
                                            var firstNumber = 0;
                                            var secondNUmber = 0;
                                            var firstNumberStr = '';
                                            var secondNUmberStr = '';

                                            // prompt user to enter two numbers
                                            firstNumberStr = prompt("Enter first number: ");

                                            while(isNaN(firstNumberStr)){
                                            firstNumberStr = prompt("Enter Correct first number: ");
                                            }

                                            secondNUmberStr = prompt("Enter a number bigger than first number: ");

                                            while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
                                            secondNUmberStr = prompt("Enter a number bigger than first number: ");
                                            }

                                            firstNumber = parseInt(firstNumberStr);
                                            secondNUmber = parseInt(secondNUmberStr);

                                            while(firstNumber <= secondNUmber){
                                            sum += firstNumber;
                                            firstNumber++
                                            }





                                            share|improve this answer












                                            This code should work for you. Variabel sum should contain your desired sum.



                                            var sum = 0;
                                            var firstNumber = 0;
                                            var secondNUmber = 0;
                                            var firstNumberStr = '';
                                            var secondNUmberStr = '';

                                            // prompt user to enter two numbers
                                            firstNumberStr = prompt("Enter first number: ");

                                            while(isNaN(firstNumberStr)){
                                            firstNumberStr = prompt("Enter Correct first number: ");
                                            }

                                            secondNUmberStr = prompt("Enter a number bigger than first number: ");

                                            while(isNaN(secondNUmberStr) || parseInt(firstNumberStr) >= parseInt(secondNUmberStr)){
                                            secondNUmberStr = prompt("Enter a number bigger than first number: ");
                                            }

                                            firstNumber = parseInt(firstNumberStr);
                                            secondNUmber = parseInt(secondNUmberStr);

                                            while(firstNumber <= secondNUmber){
                                            sum += firstNumber;
                                            firstNumber++
                                            }






                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Nov 12 at 9:03









                                            Tornike Shavishvili

                                            61021022




                                            61021022























                                                1














                                                You did a very small mistake in the code . The bug is in while loop.



                                                 while (counter < difference) {
                                                document.write(" + " + middlePoint);
                                                middlePoint = middlePoint + 1;
                                                counter++;
                                                middlePointSum += middlePoint;
                                                }


                                                you are incrementing the middlepoint before middlePointSum is Calculated. It should be incremented after calculating the middlePointSum.



                                                while (counter < difference) {
                                                document.write(" + " + middlePoint);
                                                counter++;
                                                middlePointSum += middlePoint;
                                                middlePoint = middlePoint + 1;
                                                }





                                                share|improve this answer


























                                                  1














                                                  You did a very small mistake in the code . The bug is in while loop.



                                                   while (counter < difference) {
                                                  document.write(" + " + middlePoint);
                                                  middlePoint = middlePoint + 1;
                                                  counter++;
                                                  middlePointSum += middlePoint;
                                                  }


                                                  you are incrementing the middlepoint before middlePointSum is Calculated. It should be incremented after calculating the middlePointSum.



                                                  while (counter < difference) {
                                                  document.write(" + " + middlePoint);
                                                  counter++;
                                                  middlePointSum += middlePoint;
                                                  middlePoint = middlePoint + 1;
                                                  }





                                                  share|improve this answer
























                                                    1












                                                    1








                                                    1






                                                    You did a very small mistake in the code . The bug is in while loop.



                                                     while (counter < difference) {
                                                    document.write(" + " + middlePoint);
                                                    middlePoint = middlePoint + 1;
                                                    counter++;
                                                    middlePointSum += middlePoint;
                                                    }


                                                    you are incrementing the middlepoint before middlePointSum is Calculated. It should be incremented after calculating the middlePointSum.



                                                    while (counter < difference) {
                                                    document.write(" + " + middlePoint);
                                                    counter++;
                                                    middlePointSum += middlePoint;
                                                    middlePoint = middlePoint + 1;
                                                    }





                                                    share|improve this answer












                                                    You did a very small mistake in the code . The bug is in while loop.



                                                     while (counter < difference) {
                                                    document.write(" + " + middlePoint);
                                                    middlePoint = middlePoint + 1;
                                                    counter++;
                                                    middlePointSum += middlePoint;
                                                    }


                                                    you are incrementing the middlepoint before middlePointSum is Calculated. It should be incremented after calculating the middlePointSum.



                                                    while (counter < difference) {
                                                    document.write(" + " + middlePoint);
                                                    counter++;
                                                    middlePointSum += middlePoint;
                                                    middlePoint = middlePoint + 1;
                                                    }






                                                    share|improve this answer












                                                    share|improve this answer



                                                    share|improve this answer










                                                    answered Nov 12 at 9:07









                                                    Dinesh Reddy Kancharla

                                                    255




                                                    255























                                                        1














                                                        Your middlePointSum is starting from 1 intead of 0 so you got: 5 + (7 + 8 + 9 + 10) + 10 to fix your code set middlePointSum to INITIAL_VALUE



                                                        For simplification just ignore the difference between the numbers, increment number1 until you reach number 2






                                                        var number1 = prompt("Enter first number: ");
                                                        var number2 = prompt("Enter a number bigger than first number: ");
                                                        var incNumber;
                                                        var strResult = ;

                                                        number1 = incNumber = Number(number1);
                                                        // use incNumber if you need to preserve number1 value or just use number 1 value instead
                                                        number2 = Number(number2);

                                                        var sum = 0;
                                                        while(incNumber <= number2) {
                                                        strResult.push(incNumber);
                                                        sum += incNumber++;
                                                        // this means add incNumber to sum then increment it by 1
                                                        }

                                                        document.write(`${strResult.join(' + ')} = ${sum}`);








                                                        share|improve this answer


























                                                          1














                                                          Your middlePointSum is starting from 1 intead of 0 so you got: 5 + (7 + 8 + 9 + 10) + 10 to fix your code set middlePointSum to INITIAL_VALUE



                                                          For simplification just ignore the difference between the numbers, increment number1 until you reach number 2






                                                          var number1 = prompt("Enter first number: ");
                                                          var number2 = prompt("Enter a number bigger than first number: ");
                                                          var incNumber;
                                                          var strResult = ;

                                                          number1 = incNumber = Number(number1);
                                                          // use incNumber if you need to preserve number1 value or just use number 1 value instead
                                                          number2 = Number(number2);

                                                          var sum = 0;
                                                          while(incNumber <= number2) {
                                                          strResult.push(incNumber);
                                                          sum += incNumber++;
                                                          // this means add incNumber to sum then increment it by 1
                                                          }

                                                          document.write(`${strResult.join(' + ')} = ${sum}`);








                                                          share|improve this answer
























                                                            1












                                                            1








                                                            1






                                                            Your middlePointSum is starting from 1 intead of 0 so you got: 5 + (7 + 8 + 9 + 10) + 10 to fix your code set middlePointSum to INITIAL_VALUE



                                                            For simplification just ignore the difference between the numbers, increment number1 until you reach number 2






                                                            var number1 = prompt("Enter first number: ");
                                                            var number2 = prompt("Enter a number bigger than first number: ");
                                                            var incNumber;
                                                            var strResult = ;

                                                            number1 = incNumber = Number(number1);
                                                            // use incNumber if you need to preserve number1 value or just use number 1 value instead
                                                            number2 = Number(number2);

                                                            var sum = 0;
                                                            while(incNumber <= number2) {
                                                            strResult.push(incNumber);
                                                            sum += incNumber++;
                                                            // this means add incNumber to sum then increment it by 1
                                                            }

                                                            document.write(`${strResult.join(' + ')} = ${sum}`);








                                                            share|improve this answer












                                                            Your middlePointSum is starting from 1 intead of 0 so you got: 5 + (7 + 8 + 9 + 10) + 10 to fix your code set middlePointSum to INITIAL_VALUE



                                                            For simplification just ignore the difference between the numbers, increment number1 until you reach number 2






                                                            var number1 = prompt("Enter first number: ");
                                                            var number2 = prompt("Enter a number bigger than first number: ");
                                                            var incNumber;
                                                            var strResult = ;

                                                            number1 = incNumber = Number(number1);
                                                            // use incNumber if you need to preserve number1 value or just use number 1 value instead
                                                            number2 = Number(number2);

                                                            var sum = 0;
                                                            while(incNumber <= number2) {
                                                            strResult.push(incNumber);
                                                            sum += incNumber++;
                                                            // this means add incNumber to sum then increment it by 1
                                                            }

                                                            document.write(`${strResult.join(' + ')} = ${sum}`);








                                                            var number1 = prompt("Enter first number: ");
                                                            var number2 = prompt("Enter a number bigger than first number: ");
                                                            var incNumber;
                                                            var strResult = ;

                                                            number1 = incNumber = Number(number1);
                                                            // use incNumber if you need to preserve number1 value or just use number 1 value instead
                                                            number2 = Number(number2);

                                                            var sum = 0;
                                                            while(incNumber <= number2) {
                                                            strResult.push(incNumber);
                                                            sum += incNumber++;
                                                            // this means add incNumber to sum then increment it by 1
                                                            }

                                                            document.write(`${strResult.join(' + ')} = ${sum}`);





                                                            var number1 = prompt("Enter first number: ");
                                                            var number2 = prompt("Enter a number bigger than first number: ");
                                                            var incNumber;
                                                            var strResult = ;

                                                            number1 = incNumber = Number(number1);
                                                            // use incNumber if you need to preserve number1 value or just use number 1 value instead
                                                            number2 = Number(number2);

                                                            var sum = 0;
                                                            while(incNumber <= number2) {
                                                            strResult.push(incNumber);
                                                            sum += incNumber++;
                                                            // this means add incNumber to sum then increment it by 1
                                                            }

                                                            document.write(`${strResult.join(' + ')} = ${sum}`);






                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Nov 12 at 10:55









                                                            Vanojx1

                                                            4,18021025




                                                            4,18021025























                                                                -1














                                                                One problem is that your middlePointSum does not take into account the first number after the number1, for example, the 6 in your example. Another problem is that your counter < difference test is inaccurate - you would have to iterate until counter + 1 < difference:






                                                                // declare constants
                                                                const INITIAL_VALUE = 0;
                                                                const COUNTER_VALUE = 1;

                                                                // declare variables
                                                                var number1;
                                                                var number2;
                                                                var sum;
                                                                var counter;
                                                                var difference;
                                                                var middlePoint;
                                                                var middlePointSum;

                                                                // assigning values
                                                                sum = INITIAL_VALUE;
                                                                difference = INITIAL_VALUE;
                                                                counter = COUNTER_VALUE;
                                                                middlePoint = COUNTER_VALUE;
                                                                middlePointSum = INITIAL_VALUE;

                                                                // prompt user to enter two numbers
                                                                number1 = prompt("Enter first number: ");
                                                                number2 = prompt("Enter a number bigger than first number: ");

                                                                // convert user input into numbers
                                                                number1 = Number(number1);
                                                                number2 = Number(number2);

                                                                // display number1
                                                                document.write(number1);

                                                                difference = number2 - number1;
                                                                middlePoint += number1;

                                                                middlePointSum = middlePoint;
                                                                while (counter + 1 < difference) {
                                                                document.write(" + " + middlePoint);
                                                                middlePoint = middlePoint + 1;
                                                                counter++;
                                                                console.log(middlePointSum);
                                                                middlePointSum += middlePoint;
                                                                }

                                                                // calculate the sum
                                                                sum = number1 + middlePointSum + number2;

                                                                // display number1, middle points, number2, and sum
                                                                document.write(" + " + number2 + " = " + sum);





                                                                But this seems far, far more complicated than it needs to be - why not simply iterate from number1 to number2, adding to sum along the way?






                                                                const number1 = Number(prompt("Enter first number: "));
                                                                const number2 = Number(prompt("Enter a number bigger than first number: "));
                                                                document.write(number1);
                                                                let sum = number1;
                                                                for (let i = number1 + 1; i <= number2; i++) {
                                                                sum += i;
                                                                document.write(' + ' + i);
                                                                }
                                                                document.write(' = ' + sum);





                                                                Or, create an array of the numbers, join by plus signs, and calculate the sum with reduce:






                                                                const number1 = Number(prompt("Enter first number: "));
                                                                const number2 = Number(prompt("Enter a number bigger than first number: "));

                                                                const nums = Array.from(
                                                                { length: number2 - number1 + 1 },
                                                                (_, i) => i + number1
                                                                );
                                                                const sum = nums.reduce((a, b) => a + b);
                                                                document.write(nums.join(' + ') + ' = ' + sum);








                                                                share|improve this answer


























                                                                  -1














                                                                  One problem is that your middlePointSum does not take into account the first number after the number1, for example, the 6 in your example. Another problem is that your counter < difference test is inaccurate - you would have to iterate until counter + 1 < difference:






                                                                  // declare constants
                                                                  const INITIAL_VALUE = 0;
                                                                  const COUNTER_VALUE = 1;

                                                                  // declare variables
                                                                  var number1;
                                                                  var number2;
                                                                  var sum;
                                                                  var counter;
                                                                  var difference;
                                                                  var middlePoint;
                                                                  var middlePointSum;

                                                                  // assigning values
                                                                  sum = INITIAL_VALUE;
                                                                  difference = INITIAL_VALUE;
                                                                  counter = COUNTER_VALUE;
                                                                  middlePoint = COUNTER_VALUE;
                                                                  middlePointSum = INITIAL_VALUE;

                                                                  // prompt user to enter two numbers
                                                                  number1 = prompt("Enter first number: ");
                                                                  number2 = prompt("Enter a number bigger than first number: ");

                                                                  // convert user input into numbers
                                                                  number1 = Number(number1);
                                                                  number2 = Number(number2);

                                                                  // display number1
                                                                  document.write(number1);

                                                                  difference = number2 - number1;
                                                                  middlePoint += number1;

                                                                  middlePointSum = middlePoint;
                                                                  while (counter + 1 < difference) {
                                                                  document.write(" + " + middlePoint);
                                                                  middlePoint = middlePoint + 1;
                                                                  counter++;
                                                                  console.log(middlePointSum);
                                                                  middlePointSum += middlePoint;
                                                                  }

                                                                  // calculate the sum
                                                                  sum = number1 + middlePointSum + number2;

                                                                  // display number1, middle points, number2, and sum
                                                                  document.write(" + " + number2 + " = " + sum);





                                                                  But this seems far, far more complicated than it needs to be - why not simply iterate from number1 to number2, adding to sum along the way?






                                                                  const number1 = Number(prompt("Enter first number: "));
                                                                  const number2 = Number(prompt("Enter a number bigger than first number: "));
                                                                  document.write(number1);
                                                                  let sum = number1;
                                                                  for (let i = number1 + 1; i <= number2; i++) {
                                                                  sum += i;
                                                                  document.write(' + ' + i);
                                                                  }
                                                                  document.write(' = ' + sum);





                                                                  Or, create an array of the numbers, join by plus signs, and calculate the sum with reduce:






                                                                  const number1 = Number(prompt("Enter first number: "));
                                                                  const number2 = Number(prompt("Enter a number bigger than first number: "));

                                                                  const nums = Array.from(
                                                                  { length: number2 - number1 + 1 },
                                                                  (_, i) => i + number1
                                                                  );
                                                                  const sum = nums.reduce((a, b) => a + b);
                                                                  document.write(nums.join(' + ') + ' = ' + sum);








                                                                  share|improve this answer
























                                                                    -1












                                                                    -1








                                                                    -1






                                                                    One problem is that your middlePointSum does not take into account the first number after the number1, for example, the 6 in your example. Another problem is that your counter < difference test is inaccurate - you would have to iterate until counter + 1 < difference:






                                                                    // declare constants
                                                                    const INITIAL_VALUE = 0;
                                                                    const COUNTER_VALUE = 1;

                                                                    // declare variables
                                                                    var number1;
                                                                    var number2;
                                                                    var sum;
                                                                    var counter;
                                                                    var difference;
                                                                    var middlePoint;
                                                                    var middlePointSum;

                                                                    // assigning values
                                                                    sum = INITIAL_VALUE;
                                                                    difference = INITIAL_VALUE;
                                                                    counter = COUNTER_VALUE;
                                                                    middlePoint = COUNTER_VALUE;
                                                                    middlePointSum = INITIAL_VALUE;

                                                                    // prompt user to enter two numbers
                                                                    number1 = prompt("Enter first number: ");
                                                                    number2 = prompt("Enter a number bigger than first number: ");

                                                                    // convert user input into numbers
                                                                    number1 = Number(number1);
                                                                    number2 = Number(number2);

                                                                    // display number1
                                                                    document.write(number1);

                                                                    difference = number2 - number1;
                                                                    middlePoint += number1;

                                                                    middlePointSum = middlePoint;
                                                                    while (counter + 1 < difference) {
                                                                    document.write(" + " + middlePoint);
                                                                    middlePoint = middlePoint + 1;
                                                                    counter++;
                                                                    console.log(middlePointSum);
                                                                    middlePointSum += middlePoint;
                                                                    }

                                                                    // calculate the sum
                                                                    sum = number1 + middlePointSum + number2;

                                                                    // display number1, middle points, number2, and sum
                                                                    document.write(" + " + number2 + " = " + sum);





                                                                    But this seems far, far more complicated than it needs to be - why not simply iterate from number1 to number2, adding to sum along the way?






                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));
                                                                    document.write(number1);
                                                                    let sum = number1;
                                                                    for (let i = number1 + 1; i <= number2; i++) {
                                                                    sum += i;
                                                                    document.write(' + ' + i);
                                                                    }
                                                                    document.write(' = ' + sum);





                                                                    Or, create an array of the numbers, join by plus signs, and calculate the sum with reduce:






                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));

                                                                    const nums = Array.from(
                                                                    { length: number2 - number1 + 1 },
                                                                    (_, i) => i + number1
                                                                    );
                                                                    const sum = nums.reduce((a, b) => a + b);
                                                                    document.write(nums.join(' + ') + ' = ' + sum);








                                                                    share|improve this answer












                                                                    One problem is that your middlePointSum does not take into account the first number after the number1, for example, the 6 in your example. Another problem is that your counter < difference test is inaccurate - you would have to iterate until counter + 1 < difference:






                                                                    // declare constants
                                                                    const INITIAL_VALUE = 0;
                                                                    const COUNTER_VALUE = 1;

                                                                    // declare variables
                                                                    var number1;
                                                                    var number2;
                                                                    var sum;
                                                                    var counter;
                                                                    var difference;
                                                                    var middlePoint;
                                                                    var middlePointSum;

                                                                    // assigning values
                                                                    sum = INITIAL_VALUE;
                                                                    difference = INITIAL_VALUE;
                                                                    counter = COUNTER_VALUE;
                                                                    middlePoint = COUNTER_VALUE;
                                                                    middlePointSum = INITIAL_VALUE;

                                                                    // prompt user to enter two numbers
                                                                    number1 = prompt("Enter first number: ");
                                                                    number2 = prompt("Enter a number bigger than first number: ");

                                                                    // convert user input into numbers
                                                                    number1 = Number(number1);
                                                                    number2 = Number(number2);

                                                                    // display number1
                                                                    document.write(number1);

                                                                    difference = number2 - number1;
                                                                    middlePoint += number1;

                                                                    middlePointSum = middlePoint;
                                                                    while (counter + 1 < difference) {
                                                                    document.write(" + " + middlePoint);
                                                                    middlePoint = middlePoint + 1;
                                                                    counter++;
                                                                    console.log(middlePointSum);
                                                                    middlePointSum += middlePoint;
                                                                    }

                                                                    // calculate the sum
                                                                    sum = number1 + middlePointSum + number2;

                                                                    // display number1, middle points, number2, and sum
                                                                    document.write(" + " + number2 + " = " + sum);





                                                                    But this seems far, far more complicated than it needs to be - why not simply iterate from number1 to number2, adding to sum along the way?






                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));
                                                                    document.write(number1);
                                                                    let sum = number1;
                                                                    for (let i = number1 + 1; i <= number2; i++) {
                                                                    sum += i;
                                                                    document.write(' + ' + i);
                                                                    }
                                                                    document.write(' = ' + sum);





                                                                    Or, create an array of the numbers, join by plus signs, and calculate the sum with reduce:






                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));

                                                                    const nums = Array.from(
                                                                    { length: number2 - number1 + 1 },
                                                                    (_, i) => i + number1
                                                                    );
                                                                    const sum = nums.reduce((a, b) => a + b);
                                                                    document.write(nums.join(' + ') + ' = ' + sum);








                                                                    // declare constants
                                                                    const INITIAL_VALUE = 0;
                                                                    const COUNTER_VALUE = 1;

                                                                    // declare variables
                                                                    var number1;
                                                                    var number2;
                                                                    var sum;
                                                                    var counter;
                                                                    var difference;
                                                                    var middlePoint;
                                                                    var middlePointSum;

                                                                    // assigning values
                                                                    sum = INITIAL_VALUE;
                                                                    difference = INITIAL_VALUE;
                                                                    counter = COUNTER_VALUE;
                                                                    middlePoint = COUNTER_VALUE;
                                                                    middlePointSum = INITIAL_VALUE;

                                                                    // prompt user to enter two numbers
                                                                    number1 = prompt("Enter first number: ");
                                                                    number2 = prompt("Enter a number bigger than first number: ");

                                                                    // convert user input into numbers
                                                                    number1 = Number(number1);
                                                                    number2 = Number(number2);

                                                                    // display number1
                                                                    document.write(number1);

                                                                    difference = number2 - number1;
                                                                    middlePoint += number1;

                                                                    middlePointSum = middlePoint;
                                                                    while (counter + 1 < difference) {
                                                                    document.write(" + " + middlePoint);
                                                                    middlePoint = middlePoint + 1;
                                                                    counter++;
                                                                    console.log(middlePointSum);
                                                                    middlePointSum += middlePoint;
                                                                    }

                                                                    // calculate the sum
                                                                    sum = number1 + middlePointSum + number2;

                                                                    // display number1, middle points, number2, and sum
                                                                    document.write(" + " + number2 + " = " + sum);





                                                                    // declare constants
                                                                    const INITIAL_VALUE = 0;
                                                                    const COUNTER_VALUE = 1;

                                                                    // declare variables
                                                                    var number1;
                                                                    var number2;
                                                                    var sum;
                                                                    var counter;
                                                                    var difference;
                                                                    var middlePoint;
                                                                    var middlePointSum;

                                                                    // assigning values
                                                                    sum = INITIAL_VALUE;
                                                                    difference = INITIAL_VALUE;
                                                                    counter = COUNTER_VALUE;
                                                                    middlePoint = COUNTER_VALUE;
                                                                    middlePointSum = INITIAL_VALUE;

                                                                    // prompt user to enter two numbers
                                                                    number1 = prompt("Enter first number: ");
                                                                    number2 = prompt("Enter a number bigger than first number: ");

                                                                    // convert user input into numbers
                                                                    number1 = Number(number1);
                                                                    number2 = Number(number2);

                                                                    // display number1
                                                                    document.write(number1);

                                                                    difference = number2 - number1;
                                                                    middlePoint += number1;

                                                                    middlePointSum = middlePoint;
                                                                    while (counter + 1 < difference) {
                                                                    document.write(" + " + middlePoint);
                                                                    middlePoint = middlePoint + 1;
                                                                    counter++;
                                                                    console.log(middlePointSum);
                                                                    middlePointSum += middlePoint;
                                                                    }

                                                                    // calculate the sum
                                                                    sum = number1 + middlePointSum + number2;

                                                                    // display number1, middle points, number2, and sum
                                                                    document.write(" + " + number2 + " = " + sum);





                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));
                                                                    document.write(number1);
                                                                    let sum = number1;
                                                                    for (let i = number1 + 1; i <= number2; i++) {
                                                                    sum += i;
                                                                    document.write(' + ' + i);
                                                                    }
                                                                    document.write(' = ' + sum);





                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));
                                                                    document.write(number1);
                                                                    let sum = number1;
                                                                    for (let i = number1 + 1; i <= number2; i++) {
                                                                    sum += i;
                                                                    document.write(' + ' + i);
                                                                    }
                                                                    document.write(' = ' + sum);





                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));

                                                                    const nums = Array.from(
                                                                    { length: number2 - number1 + 1 },
                                                                    (_, i) => i + number1
                                                                    );
                                                                    const sum = nums.reduce((a, b) => a + b);
                                                                    document.write(nums.join(' + ') + ' = ' + sum);





                                                                    const number1 = Number(prompt("Enter first number: "));
                                                                    const number2 = Number(prompt("Enter a number bigger than first number: "));

                                                                    const nums = Array.from(
                                                                    { length: number2 - number1 + 1 },
                                                                    (_, i) => i + number1
                                                                    );
                                                                    const sum = nums.reduce((a, b) => a + b);
                                                                    document.write(nums.join(' + ') + ' = ' + sum);






                                                                    share|improve this answer












                                                                    share|improve this answer



                                                                    share|improve this answer










                                                                    answered Nov 12 at 8:54









                                                                    CertainPerformance

                                                                    75.2k143659




                                                                    75.2k143659























                                                                        -1

















                                                                        var num1 = Number(prompt("Enter a number"));
                                                                        var num2 = Number(prompt("Enter a larger number"));


                                                                        document.write(num1 + "+" + num2 + "=" + (num1+num2));



                                                                        // now lets find the sum of numbers between these two numbers.
                                                                        document.write("<br>"); // new line

                                                                        var counter = num1; // we will start countring from the smaller number
                                                                        var sum = 0;

                                                                        //keep looping until we reach the bigger number (num2)
                                                                        while(counter<=num2) {
                                                                        sum += counter;
                                                                        // print the current number
                                                                        document.write(counter);
                                                                        //print a plus sign
                                                                        if(counter<num2) {
                                                                        // only put a (plus sign) if we are not yet reached num2
                                                                        document.write("+");
                                                                        }

                                                                        //increment to control the loop
                                                                        counter ++;

                                                                        }


                                                                        //finally put the result
                                                                        document.write("=" + sum);





                                                                        You are setting counter to the constant COUNTER_VALUE which is 1.



                                                                        Then, in your program, you are entering 5 and 6 as input. The difference is 6-5 which is 1.



                                                                        Your while loop has a condition to only execute when counter<difference . In your case, counter=1 and difference=1. This means that they are equal, and thus, the loop will not execute.



                                                                        I hope this sheds light on what needs to be done from your side to fix this bug.






                                                                        share|improve this answer




























                                                                          -1

















                                                                          var num1 = Number(prompt("Enter a number"));
                                                                          var num2 = Number(prompt("Enter a larger number"));


                                                                          document.write(num1 + "+" + num2 + "=" + (num1+num2));



                                                                          // now lets find the sum of numbers between these two numbers.
                                                                          document.write("<br>"); // new line

                                                                          var counter = num1; // we will start countring from the smaller number
                                                                          var sum = 0;

                                                                          //keep looping until we reach the bigger number (num2)
                                                                          while(counter<=num2) {
                                                                          sum += counter;
                                                                          // print the current number
                                                                          document.write(counter);
                                                                          //print a plus sign
                                                                          if(counter<num2) {
                                                                          // only put a (plus sign) if we are not yet reached num2
                                                                          document.write("+");
                                                                          }

                                                                          //increment to control the loop
                                                                          counter ++;

                                                                          }


                                                                          //finally put the result
                                                                          document.write("=" + sum);





                                                                          You are setting counter to the constant COUNTER_VALUE which is 1.



                                                                          Then, in your program, you are entering 5 and 6 as input. The difference is 6-5 which is 1.



                                                                          Your while loop has a condition to only execute when counter<difference . In your case, counter=1 and difference=1. This means that they are equal, and thus, the loop will not execute.



                                                                          I hope this sheds light on what needs to be done from your side to fix this bug.






                                                                          share|improve this answer


























                                                                            -1












                                                                            -1








                                                                            -1









                                                                            var num1 = Number(prompt("Enter a number"));
                                                                            var num2 = Number(prompt("Enter a larger number"));


                                                                            document.write(num1 + "+" + num2 + "=" + (num1+num2));



                                                                            // now lets find the sum of numbers between these two numbers.
                                                                            document.write("<br>"); // new line

                                                                            var counter = num1; // we will start countring from the smaller number
                                                                            var sum = 0;

                                                                            //keep looping until we reach the bigger number (num2)
                                                                            while(counter<=num2) {
                                                                            sum += counter;
                                                                            // print the current number
                                                                            document.write(counter);
                                                                            //print a plus sign
                                                                            if(counter<num2) {
                                                                            // only put a (plus sign) if we are not yet reached num2
                                                                            document.write("+");
                                                                            }

                                                                            //increment to control the loop
                                                                            counter ++;

                                                                            }


                                                                            //finally put the result
                                                                            document.write("=" + sum);





                                                                            You are setting counter to the constant COUNTER_VALUE which is 1.



                                                                            Then, in your program, you are entering 5 and 6 as input. The difference is 6-5 which is 1.



                                                                            Your while loop has a condition to only execute when counter<difference . In your case, counter=1 and difference=1. This means that they are equal, and thus, the loop will not execute.



                                                                            I hope this sheds light on what needs to be done from your side to fix this bug.






                                                                            share|improve this answer

















                                                                            var num1 = Number(prompt("Enter a number"));
                                                                            var num2 = Number(prompt("Enter a larger number"));


                                                                            document.write(num1 + "+" + num2 + "=" + (num1+num2));



                                                                            // now lets find the sum of numbers between these two numbers.
                                                                            document.write("<br>"); // new line

                                                                            var counter = num1; // we will start countring from the smaller number
                                                                            var sum = 0;

                                                                            //keep looping until we reach the bigger number (num2)
                                                                            while(counter<=num2) {
                                                                            sum += counter;
                                                                            // print the current number
                                                                            document.write(counter);
                                                                            //print a plus sign
                                                                            if(counter<num2) {
                                                                            // only put a (plus sign) if we are not yet reached num2
                                                                            document.write("+");
                                                                            }

                                                                            //increment to control the loop
                                                                            counter ++;

                                                                            }


                                                                            //finally put the result
                                                                            document.write("=" + sum);





                                                                            You are setting counter to the constant COUNTER_VALUE which is 1.



                                                                            Then, in your program, you are entering 5 and 6 as input. The difference is 6-5 which is 1.



                                                                            Your while loop has a condition to only execute when counter<difference . In your case, counter=1 and difference=1. This means that they are equal, and thus, the loop will not execute.



                                                                            I hope this sheds light on what needs to be done from your side to fix this bug.






                                                                            var num1 = Number(prompt("Enter a number"));
                                                                            var num2 = Number(prompt("Enter a larger number"));


                                                                            document.write(num1 + "+" + num2 + "=" + (num1+num2));



                                                                            // now lets find the sum of numbers between these two numbers.
                                                                            document.write("<br>"); // new line

                                                                            var counter = num1; // we will start countring from the smaller number
                                                                            var sum = 0;

                                                                            //keep looping until we reach the bigger number (num2)
                                                                            while(counter<=num2) {
                                                                            sum += counter;
                                                                            // print the current number
                                                                            document.write(counter);
                                                                            //print a plus sign
                                                                            if(counter<num2) {
                                                                            // only put a (plus sign) if we are not yet reached num2
                                                                            document.write("+");
                                                                            }

                                                                            //increment to control the loop
                                                                            counter ++;

                                                                            }


                                                                            //finally put the result
                                                                            document.write("=" + sum);





                                                                            var num1 = Number(prompt("Enter a number"));
                                                                            var num2 = Number(prompt("Enter a larger number"));


                                                                            document.write(num1 + "+" + num2 + "=" + (num1+num2));



                                                                            // now lets find the sum of numbers between these two numbers.
                                                                            document.write("<br>"); // new line

                                                                            var counter = num1; // we will start countring from the smaller number
                                                                            var sum = 0;

                                                                            //keep looping until we reach the bigger number (num2)
                                                                            while(counter<=num2) {
                                                                            sum += counter;
                                                                            // print the current number
                                                                            document.write(counter);
                                                                            //print a plus sign
                                                                            if(counter<num2) {
                                                                            // only put a (plus sign) if we are not yet reached num2
                                                                            document.write("+");
                                                                            }

                                                                            //increment to control the loop
                                                                            counter ++;

                                                                            }


                                                                            //finally put the result
                                                                            document.write("=" + sum);






                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited Nov 12 at 9:09

























                                                                            answered Nov 12 at 8:55









                                                                            Ahmad

                                                                            8,20743463




                                                                            8,20743463






























                                                                                draft saved

                                                                                draft discarded




















































                                                                                Thanks for contributing an answer to Stack Overflow!


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.





                                                                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                Please pay close attention to the following guidance:


                                                                                • Please be sure to answer the question. Provide details and share your research!

                                                                                But avoid



                                                                                • Asking for help, clarification, or responding to other answers.

                                                                                • Making statements based on opinion; back them up with references or personal experience.


                                                                                To learn more, see our tips on writing great answers.




                                                                                draft saved


                                                                                draft discarded














                                                                                StackExchange.ready(
                                                                                function () {
                                                                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258500%2fsum-of-two-numbers-and-the-numbers-between-them%23new-answer', 'question_page');
                                                                                }
                                                                                );

                                                                                Post as a guest















                                                                                Required, but never shown





















































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown

































                                                                                Required, but never shown














                                                                                Required, but never shown












                                                                                Required, but never shown







                                                                                Required, but never shown







                                                                                Popular posts from this blog

                                                                                Florida Star v. B. J. F.

                                                                                Danny Elfman

                                                                                Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues