Multiply all numbers between given two numbers in for loops










0















I ask user for first number. Then I ask for a number bigger than first. Multiply all the numbers from the first number to the second and output the results.



I have it working as a while loop but need to switch it into a for loop. I keep getting the wrong value for multiply when I attempt to switch it into a for loop. Not sure what is needed to be done.



const INITIAL_VALUE = 0;

// declare variables
var number1;
var number2;
var multiply;
var middlePoint;

// initialize sum
multiply = 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);

// assign middlePoint
middlePoint = number1;

// display number1
document.write(number1);

// assign multiply and increment middlePoint
multiply = middlePoint;
middlePoint++;

// while loop to display numbers in between and multiply
//**NEEDS TO BE A FOR LOOP, NOT A WHILE LOOP**
while (middlePoint < number2)
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;


// add multiply to number2 for final multiplication total
multiply *= number2;

// display number2 and final multiplication total
document.write(" * " + number2 + " = " + multiply);









share|improve this question
























  • there is no for loop in your code

    – Eriks Klotins
    Nov 13 '18 at 8:06















0















I ask user for first number. Then I ask for a number bigger than first. Multiply all the numbers from the first number to the second and output the results.



I have it working as a while loop but need to switch it into a for loop. I keep getting the wrong value for multiply when I attempt to switch it into a for loop. Not sure what is needed to be done.



const INITIAL_VALUE = 0;

// declare variables
var number1;
var number2;
var multiply;
var middlePoint;

// initialize sum
multiply = 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);

// assign middlePoint
middlePoint = number1;

// display number1
document.write(number1);

// assign multiply and increment middlePoint
multiply = middlePoint;
middlePoint++;

// while loop to display numbers in between and multiply
//**NEEDS TO BE A FOR LOOP, NOT A WHILE LOOP**
while (middlePoint < number2)
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;


// add multiply to number2 for final multiplication total
multiply *= number2;

// display number2 and final multiplication total
document.write(" * " + number2 + " = " + multiply);









share|improve this question
























  • there is no for loop in your code

    – Eriks Klotins
    Nov 13 '18 at 8:06













0












0








0








I ask user for first number. Then I ask for a number bigger than first. Multiply all the numbers from the first number to the second and output the results.



I have it working as a while loop but need to switch it into a for loop. I keep getting the wrong value for multiply when I attempt to switch it into a for loop. Not sure what is needed to be done.



const INITIAL_VALUE = 0;

// declare variables
var number1;
var number2;
var multiply;
var middlePoint;

// initialize sum
multiply = 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);

// assign middlePoint
middlePoint = number1;

// display number1
document.write(number1);

// assign multiply and increment middlePoint
multiply = middlePoint;
middlePoint++;

// while loop to display numbers in between and multiply
//**NEEDS TO BE A FOR LOOP, NOT A WHILE LOOP**
while (middlePoint < number2)
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;


// add multiply to number2 for final multiplication total
multiply *= number2;

// display number2 and final multiplication total
document.write(" * " + number2 + " = " + multiply);









share|improve this question
















I ask user for first number. Then I ask for a number bigger than first. Multiply all the numbers from the first number to the second and output the results.



I have it working as a while loop but need to switch it into a for loop. I keep getting the wrong value for multiply when I attempt to switch it into a for loop. Not sure what is needed to be done.



const INITIAL_VALUE = 0;

// declare variables
var number1;
var number2;
var multiply;
var middlePoint;

// initialize sum
multiply = 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);

// assign middlePoint
middlePoint = number1;

// display number1
document.write(number1);

// assign multiply and increment middlePoint
multiply = middlePoint;
middlePoint++;

// while loop to display numbers in between and multiply
//**NEEDS TO BE A FOR LOOP, NOT A WHILE LOOP**
while (middlePoint < number2)
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;


// add multiply to number2 for final multiplication total
multiply *= number2;

// display number2 and final multiplication total
document.write(" * " + number2 + " = " + multiply);






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 8:33









Dharmang

2,5802734




2,5802734










asked Nov 13 '18 at 8:03









M.P.T.M.P.T.

427




427












  • there is no for loop in your code

    – Eriks Klotins
    Nov 13 '18 at 8:06

















  • there is no for loop in your code

    – Eriks Klotins
    Nov 13 '18 at 8:06
















there is no for loop in your code

– Eriks Klotins
Nov 13 '18 at 8:06





there is no for loop in your code

– Eriks Klotins
Nov 13 '18 at 8:06












6 Answers
6






active

oldest

votes


















0














Welcome to StackOverflow!



For switching to for loop, simply make sure that the number of loop is the same as the while loop. In this case, the number of loop to be made is number2 - number1 - 1 or the amount of number between number2 and number1. Therefore, your for loop will be:



for (var i=0; i<number2-number1-1; i++) 
document.write(" * " + middlePoint);
multiply *= middlePoint;
middlePoint++;






share|improve this answer






























    0














    Currently, your code is written to take a while loop. Transitioning to a for loop is messier than using a while loop, as you're checking a Boolean condition. However, if you really want a for loop, here you go:



    for (;middlePoint < number2;) 
    document.write(" * " + middlePoint);
    multiply *= middlePoint;
    middlePoint++;






    share|improve this answer






























      0














      You could use following code to multiply all numbers from n1 to n2: mul will contain the multiplication result:






      var mul = 1;
      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);

      for(;firstNumber <= secondNUmber;)
      mul *= firstNumber;
      firstNumber++


      alert(mul);








      share|improve this answer






























        0














        Your code can be modified as below:






        // declare variables
        var number1;
        var number2;
        var multiply;
        var middlePoint;
        const INITIAL_VALUE = 0;
        // initialize sum
        multiply = 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);

        // assign middlePoint
        middlePoint = number1;

        // display number1
        document.write(number1);

        // assign multiply and increment middlePoint
        multiply = middlePoint;
        middlePoint++;

        // while loop to display numbers in between and multiply
        for (var i=0;middlePoint < number2; i++)
        document.write(" * " + middlePoint);
        multiply *= middlePoint;
        middlePoint++;


        // add multiply to number2 for final multiplication total
        multiply *= number2;

        // display number2 and final multiplication total
        document.write(" * " + number2 + " = " + multiply);








        share|improve this answer























        • what is the need of variable i, isnt middlePoint enough to serve the purpose?

          – Subhasish Bhattacharjee
          Nov 13 '18 at 8:23











        • It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

          – Sreekandan K
          Nov 13 '18 at 8:32












        • The above code is working well after converting while loop into for loop

          – Sreekandan K
          Nov 13 '18 at 8:33











        • A working code is not always a good code!

          – Subhasish Bhattacharjee
          Nov 13 '18 at 8:34


















        0














        You can do:






        let number1, number2, result, resultText;

        // 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);

        // Initialize result and resultText
        result = number1;
        resultText = '' + number1;

        // for loop to display numbers in between and multiply
        for (let i = 1; i <= number2 - number1; i++)
        const temp = number1 + i;
        result *= temp;
        resultText += ' * ' + temp;


        // display final multiplication total
        resultText += ' = ' + result;
        document.write(resultText);








        share|improve this answer
































          0














          Here is finding the product of all numbers between given two numbers in for loops,
          simplified code






          var num1 = 10;
          var num2 = 20;
          var product = 1;

          document.write(num1);
          num1 ++;

          for (;num1 <= num2;)
          document.write(" * " + num1);
          product *= num1;
          num1++;


          // display final product
          document.write(" = " + product);








          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%2f53276367%2fmultiply-all-numbers-between-given-two-numbers-in-for-loops%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Welcome to StackOverflow!



            For switching to for loop, simply make sure that the number of loop is the same as the while loop. In this case, the number of loop to be made is number2 - number1 - 1 or the amount of number between number2 and number1. Therefore, your for loop will be:



            for (var i=0; i<number2-number1-1; i++) 
            document.write(" * " + middlePoint);
            multiply *= middlePoint;
            middlePoint++;






            share|improve this answer



























              0














              Welcome to StackOverflow!



              For switching to for loop, simply make sure that the number of loop is the same as the while loop. In this case, the number of loop to be made is number2 - number1 - 1 or the amount of number between number2 and number1. Therefore, your for loop will be:



              for (var i=0; i<number2-number1-1; i++) 
              document.write(" * " + middlePoint);
              multiply *= middlePoint;
              middlePoint++;






              share|improve this answer

























                0












                0








                0







                Welcome to StackOverflow!



                For switching to for loop, simply make sure that the number of loop is the same as the while loop. In this case, the number of loop to be made is number2 - number1 - 1 or the amount of number between number2 and number1. Therefore, your for loop will be:



                for (var i=0; i<number2-number1-1; i++) 
                document.write(" * " + middlePoint);
                multiply *= middlePoint;
                middlePoint++;






                share|improve this answer













                Welcome to StackOverflow!



                For switching to for loop, simply make sure that the number of loop is the same as the while loop. In this case, the number of loop to be made is number2 - number1 - 1 or the amount of number between number2 and number1. Therefore, your for loop will be:



                for (var i=0; i<number2-number1-1; i++) 
                document.write(" * " + middlePoint);
                multiply *= middlePoint;
                middlePoint++;







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 8:09









                AndreasAndreas

                2,05731323




                2,05731323























                    0














                    Currently, your code is written to take a while loop. Transitioning to a for loop is messier than using a while loop, as you're checking a Boolean condition. However, if you really want a for loop, here you go:



                    for (;middlePoint < number2;) 
                    document.write(" * " + middlePoint);
                    multiply *= middlePoint;
                    middlePoint++;






                    share|improve this answer



























                      0














                      Currently, your code is written to take a while loop. Transitioning to a for loop is messier than using a while loop, as you're checking a Boolean condition. However, if you really want a for loop, here you go:



                      for (;middlePoint < number2;) 
                      document.write(" * " + middlePoint);
                      multiply *= middlePoint;
                      middlePoint++;






                      share|improve this answer

























                        0












                        0








                        0







                        Currently, your code is written to take a while loop. Transitioning to a for loop is messier than using a while loop, as you're checking a Boolean condition. However, if you really want a for loop, here you go:



                        for (;middlePoint < number2;) 
                        document.write(" * " + middlePoint);
                        multiply *= middlePoint;
                        middlePoint++;






                        share|improve this answer













                        Currently, your code is written to take a while loop. Transitioning to a for loop is messier than using a while loop, as you're checking a Boolean condition. However, if you really want a for loop, here you go:



                        for (;middlePoint < number2;) 
                        document.write(" * " + middlePoint);
                        multiply *= middlePoint;
                        middlePoint++;







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 13 '18 at 8:09









                        Jack BashfordJack Bashford

                        12.6k31847




                        12.6k31847





















                            0














                            You could use following code to multiply all numbers from n1 to n2: mul will contain the multiplication result:






                            var mul = 1;
                            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);

                            for(;firstNumber <= secondNUmber;)
                            mul *= firstNumber;
                            firstNumber++


                            alert(mul);








                            share|improve this answer



























                              0














                              You could use following code to multiply all numbers from n1 to n2: mul will contain the multiplication result:






                              var mul = 1;
                              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);

                              for(;firstNumber <= secondNUmber;)
                              mul *= firstNumber;
                              firstNumber++


                              alert(mul);








                              share|improve this answer

























                                0












                                0








                                0







                                You could use following code to multiply all numbers from n1 to n2: mul will contain the multiplication result:






                                var mul = 1;
                                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);

                                for(;firstNumber <= secondNUmber;)
                                mul *= firstNumber;
                                firstNumber++


                                alert(mul);








                                share|improve this answer













                                You could use following code to multiply all numbers from n1 to n2: mul will contain the multiplication result:






                                var mul = 1;
                                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);

                                for(;firstNumber <= secondNUmber;)
                                mul *= firstNumber;
                                firstNumber++


                                alert(mul);








                                var mul = 1;
                                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);

                                for(;firstNumber <= secondNUmber;)
                                mul *= firstNumber;
                                firstNumber++


                                alert(mul);





                                var mul = 1;
                                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);

                                for(;firstNumber <= secondNUmber;)
                                mul *= firstNumber;
                                firstNumber++


                                alert(mul);






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 13 '18 at 8:18









                                Tornike ShavishviliTornike Shavishvili

                                61021122




                                61021122





















                                    0














                                    Your code can be modified as below:






                                    // declare variables
                                    var number1;
                                    var number2;
                                    var multiply;
                                    var middlePoint;
                                    const INITIAL_VALUE = 0;
                                    // initialize sum
                                    multiply = 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);

                                    // assign middlePoint
                                    middlePoint = number1;

                                    // display number1
                                    document.write(number1);

                                    // assign multiply and increment middlePoint
                                    multiply = middlePoint;
                                    middlePoint++;

                                    // while loop to display numbers in between and multiply
                                    for (var i=0;middlePoint < number2; i++)
                                    document.write(" * " + middlePoint);
                                    multiply *= middlePoint;
                                    middlePoint++;


                                    // add multiply to number2 for final multiplication total
                                    multiply *= number2;

                                    // display number2 and final multiplication total
                                    document.write(" * " + number2 + " = " + multiply);








                                    share|improve this answer























                                    • what is the need of variable i, isnt middlePoint enough to serve the purpose?

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:23











                                    • It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

                                      – Sreekandan K
                                      Nov 13 '18 at 8:32












                                    • The above code is working well after converting while loop into for loop

                                      – Sreekandan K
                                      Nov 13 '18 at 8:33











                                    • A working code is not always a good code!

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:34















                                    0














                                    Your code can be modified as below:






                                    // declare variables
                                    var number1;
                                    var number2;
                                    var multiply;
                                    var middlePoint;
                                    const INITIAL_VALUE = 0;
                                    // initialize sum
                                    multiply = 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);

                                    // assign middlePoint
                                    middlePoint = number1;

                                    // display number1
                                    document.write(number1);

                                    // assign multiply and increment middlePoint
                                    multiply = middlePoint;
                                    middlePoint++;

                                    // while loop to display numbers in between and multiply
                                    for (var i=0;middlePoint < number2; i++)
                                    document.write(" * " + middlePoint);
                                    multiply *= middlePoint;
                                    middlePoint++;


                                    // add multiply to number2 for final multiplication total
                                    multiply *= number2;

                                    // display number2 and final multiplication total
                                    document.write(" * " + number2 + " = " + multiply);








                                    share|improve this answer























                                    • what is the need of variable i, isnt middlePoint enough to serve the purpose?

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:23











                                    • It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

                                      – Sreekandan K
                                      Nov 13 '18 at 8:32












                                    • The above code is working well after converting while loop into for loop

                                      – Sreekandan K
                                      Nov 13 '18 at 8:33











                                    • A working code is not always a good code!

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:34













                                    0












                                    0








                                    0







                                    Your code can be modified as below:






                                    // declare variables
                                    var number1;
                                    var number2;
                                    var multiply;
                                    var middlePoint;
                                    const INITIAL_VALUE = 0;
                                    // initialize sum
                                    multiply = 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);

                                    // assign middlePoint
                                    middlePoint = number1;

                                    // display number1
                                    document.write(number1);

                                    // assign multiply and increment middlePoint
                                    multiply = middlePoint;
                                    middlePoint++;

                                    // while loop to display numbers in between and multiply
                                    for (var i=0;middlePoint < number2; i++)
                                    document.write(" * " + middlePoint);
                                    multiply *= middlePoint;
                                    middlePoint++;


                                    // add multiply to number2 for final multiplication total
                                    multiply *= number2;

                                    // display number2 and final multiplication total
                                    document.write(" * " + number2 + " = " + multiply);








                                    share|improve this answer













                                    Your code can be modified as below:






                                    // declare variables
                                    var number1;
                                    var number2;
                                    var multiply;
                                    var middlePoint;
                                    const INITIAL_VALUE = 0;
                                    // initialize sum
                                    multiply = 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);

                                    // assign middlePoint
                                    middlePoint = number1;

                                    // display number1
                                    document.write(number1);

                                    // assign multiply and increment middlePoint
                                    multiply = middlePoint;
                                    middlePoint++;

                                    // while loop to display numbers in between and multiply
                                    for (var i=0;middlePoint < number2; i++)
                                    document.write(" * " + middlePoint);
                                    multiply *= middlePoint;
                                    middlePoint++;


                                    // add multiply to number2 for final multiplication total
                                    multiply *= number2;

                                    // display number2 and final multiplication total
                                    document.write(" * " + number2 + " = " + multiply);








                                    // declare variables
                                    var number1;
                                    var number2;
                                    var multiply;
                                    var middlePoint;
                                    const INITIAL_VALUE = 0;
                                    // initialize sum
                                    multiply = 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);

                                    // assign middlePoint
                                    middlePoint = number1;

                                    // display number1
                                    document.write(number1);

                                    // assign multiply and increment middlePoint
                                    multiply = middlePoint;
                                    middlePoint++;

                                    // while loop to display numbers in between and multiply
                                    for (var i=0;middlePoint < number2; i++)
                                    document.write(" * " + middlePoint);
                                    multiply *= middlePoint;
                                    middlePoint++;


                                    // add multiply to number2 for final multiplication total
                                    multiply *= number2;

                                    // display number2 and final multiplication total
                                    document.write(" * " + number2 + " = " + multiply);





                                    // declare variables
                                    var number1;
                                    var number2;
                                    var multiply;
                                    var middlePoint;
                                    const INITIAL_VALUE = 0;
                                    // initialize sum
                                    multiply = 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);

                                    // assign middlePoint
                                    middlePoint = number1;

                                    // display number1
                                    document.write(number1);

                                    // assign multiply and increment middlePoint
                                    multiply = middlePoint;
                                    middlePoint++;

                                    // while loop to display numbers in between and multiply
                                    for (var i=0;middlePoint < number2; i++)
                                    document.write(" * " + middlePoint);
                                    multiply *= middlePoint;
                                    middlePoint++;


                                    // add multiply to number2 for final multiplication total
                                    multiply *= number2;

                                    // display number2 and final multiplication total
                                    document.write(" * " + number2 + " = " + multiply);






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 13 '18 at 8:21









                                    Sreekandan KSreekandan K

                                    1




                                    1












                                    • what is the need of variable i, isnt middlePoint enough to serve the purpose?

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:23











                                    • It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

                                      – Sreekandan K
                                      Nov 13 '18 at 8:32












                                    • The above code is working well after converting while loop into for loop

                                      – Sreekandan K
                                      Nov 13 '18 at 8:33











                                    • A working code is not always a good code!

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:34

















                                    • what is the need of variable i, isnt middlePoint enough to serve the purpose?

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:23











                                    • It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

                                      – Sreekandan K
                                      Nov 13 '18 at 8:32












                                    • The above code is working well after converting while loop into for loop

                                      – Sreekandan K
                                      Nov 13 '18 at 8:33











                                    • A working code is not always a good code!

                                      – Subhasish Bhattacharjee
                                      Nov 13 '18 at 8:34
















                                    what is the need of variable i, isnt middlePoint enough to serve the purpose?

                                    – Subhasish Bhattacharjee
                                    Nov 13 '18 at 8:23





                                    what is the need of variable i, isnt middlePoint enough to serve the purpose?

                                    – Subhasish Bhattacharjee
                                    Nov 13 '18 at 8:23













                                    It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

                                    – Sreekandan K
                                    Nov 13 '18 at 8:32






                                    It is just a for loop variable. As the code on the original post is working fine, it does not require to modify his code. So just I used for loop in the place of while loop..

                                    – Sreekandan K
                                    Nov 13 '18 at 8:32














                                    The above code is working well after converting while loop into for loop

                                    – Sreekandan K
                                    Nov 13 '18 at 8:33





                                    The above code is working well after converting while loop into for loop

                                    – Sreekandan K
                                    Nov 13 '18 at 8:33













                                    A working code is not always a good code!

                                    – Subhasish Bhattacharjee
                                    Nov 13 '18 at 8:34





                                    A working code is not always a good code!

                                    – Subhasish Bhattacharjee
                                    Nov 13 '18 at 8:34











                                    0














                                    You can do:






                                    let number1, number2, result, resultText;

                                    // 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);

                                    // Initialize result and resultText
                                    result = number1;
                                    resultText = '' + number1;

                                    // for loop to display numbers in between and multiply
                                    for (let i = 1; i <= number2 - number1; i++)
                                    const temp = number1 + i;
                                    result *= temp;
                                    resultText += ' * ' + temp;


                                    // display final multiplication total
                                    resultText += ' = ' + result;
                                    document.write(resultText);








                                    share|improve this answer





























                                      0














                                      You can do:






                                      let number1, number2, result, resultText;

                                      // 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);

                                      // Initialize result and resultText
                                      result = number1;
                                      resultText = '' + number1;

                                      // for loop to display numbers in between and multiply
                                      for (let i = 1; i <= number2 - number1; i++)
                                      const temp = number1 + i;
                                      result *= temp;
                                      resultText += ' * ' + temp;


                                      // display final multiplication total
                                      resultText += ' = ' + result;
                                      document.write(resultText);








                                      share|improve this answer



























                                        0












                                        0








                                        0







                                        You can do:






                                        let number1, number2, result, resultText;

                                        // 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);

                                        // Initialize result and resultText
                                        result = number1;
                                        resultText = '' + number1;

                                        // for loop to display numbers in between and multiply
                                        for (let i = 1; i <= number2 - number1; i++)
                                        const temp = number1 + i;
                                        result *= temp;
                                        resultText += ' * ' + temp;


                                        // display final multiplication total
                                        resultText += ' = ' + result;
                                        document.write(resultText);








                                        share|improve this answer















                                        You can do:






                                        let number1, number2, result, resultText;

                                        // 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);

                                        // Initialize result and resultText
                                        result = number1;
                                        resultText = '' + number1;

                                        // for loop to display numbers in between and multiply
                                        for (let i = 1; i <= number2 - number1; i++)
                                        const temp = number1 + i;
                                        result *= temp;
                                        resultText += ' * ' + temp;


                                        // display final multiplication total
                                        resultText += ' = ' + result;
                                        document.write(resultText);








                                        let number1, number2, result, resultText;

                                        // 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);

                                        // Initialize result and resultText
                                        result = number1;
                                        resultText = '' + number1;

                                        // for loop to display numbers in between and multiply
                                        for (let i = 1; i <= number2 - number1; i++)
                                        const temp = number1 + i;
                                        result *= temp;
                                        resultText += ' * ' + temp;


                                        // display final multiplication total
                                        resultText += ' = ' + result;
                                        document.write(resultText);





                                        let number1, number2, result, resultText;

                                        // 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);

                                        // Initialize result and resultText
                                        result = number1;
                                        resultText = '' + number1;

                                        // for loop to display numbers in between and multiply
                                        for (let i = 1; i <= number2 - number1; i++)
                                        const temp = number1 + i;
                                        result *= temp;
                                        resultText += ' * ' + temp;


                                        // display final multiplication total
                                        resultText += ' = ' + result;
                                        document.write(resultText);






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Nov 13 '18 at 8:47

























                                        answered Nov 13 '18 at 8:40









                                        Yosvel QuinteroYosvel Quintero

                                        11.8k42531




                                        11.8k42531





















                                            0














                                            Here is finding the product of all numbers between given two numbers in for loops,
                                            simplified code






                                            var num1 = 10;
                                            var num2 = 20;
                                            var product = 1;

                                            document.write(num1);
                                            num1 ++;

                                            for (;num1 <= num2;)
                                            document.write(" * " + num1);
                                            product *= num1;
                                            num1++;


                                            // display final product
                                            document.write(" = " + product);








                                            share|improve this answer



























                                              0














                                              Here is finding the product of all numbers between given two numbers in for loops,
                                              simplified code






                                              var num1 = 10;
                                              var num2 = 20;
                                              var product = 1;

                                              document.write(num1);
                                              num1 ++;

                                              for (;num1 <= num2;)
                                              document.write(" * " + num1);
                                              product *= num1;
                                              num1++;


                                              // display final product
                                              document.write(" = " + product);








                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                Here is finding the product of all numbers between given two numbers in for loops,
                                                simplified code






                                                var num1 = 10;
                                                var num2 = 20;
                                                var product = 1;

                                                document.write(num1);
                                                num1 ++;

                                                for (;num1 <= num2;)
                                                document.write(" * " + num1);
                                                product *= num1;
                                                num1++;


                                                // display final product
                                                document.write(" = " + product);








                                                share|improve this answer













                                                Here is finding the product of all numbers between given two numbers in for loops,
                                                simplified code






                                                var num1 = 10;
                                                var num2 = 20;
                                                var product = 1;

                                                document.write(num1);
                                                num1 ++;

                                                for (;num1 <= num2;)
                                                document.write(" * " + num1);
                                                product *= num1;
                                                num1++;


                                                // display final product
                                                document.write(" = " + product);








                                                var num1 = 10;
                                                var num2 = 20;
                                                var product = 1;

                                                document.write(num1);
                                                num1 ++;

                                                for (;num1 <= num2;)
                                                document.write(" * " + num1);
                                                product *= num1;
                                                num1++;


                                                // display final product
                                                document.write(" = " + product);





                                                var num1 = 10;
                                                var num2 = 20;
                                                var product = 1;

                                                document.write(num1);
                                                num1 ++;

                                                for (;num1 <= num2;)
                                                document.write(" * " + num1);
                                                product *= num1;
                                                num1++;


                                                // display final product
                                                document.write(" = " + product);






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 13 '18 at 9:06









                                                Ajmal HassanAjmal Hassan

                                                7619




                                                7619



























                                                    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.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276367%2fmultiply-all-numbers-between-given-two-numbers-in-for-loops%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

                                                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                                    Crossroads (UK TV series)

                                                    ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế