Multiply all numbers between given two numbers in for loops
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
add a comment |
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
there is no for loop in your code
– Eriks Klotins
Nov 13 '18 at 8:06
add a comment |
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
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
javascript
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
add a comment |
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
add a comment |
6 Answers
6
active
oldest
votes
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++;
add a comment |
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++;
add a comment |
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);
add a comment |
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);
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
add a comment |
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);
add a comment |
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);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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++;
add a comment |
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++;
add a comment |
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++;
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++;
answered Nov 13 '18 at 8:09
AndreasAndreas
2,05731323
2,05731323
add a comment |
add a comment |
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++;
add a comment |
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++;
add a comment |
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++;
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++;
answered Nov 13 '18 at 8:09
Jack BashfordJack Bashford
12.6k31847
12.6k31847
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 13 '18 at 8:18
Tornike ShavishviliTornike Shavishvili
61021122
61021122
add a comment |
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
edited Nov 13 '18 at 8:47
answered Nov 13 '18 at 8:40
Yosvel QuinteroYosvel Quintero
11.8k42531
11.8k42531
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 13 '18 at 9:06
Ajmal HassanAjmal Hassan
7619
7619
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
there is no for loop in your code
– Eriks Klotins
Nov 13 '18 at 8:06