0

How to print sum of a user input in JavaScript loops?

Hi I want something like this var one= prompt("Please enter a number"); var two= prompt("Please enter a number"); var sum = one+ two for(i = 1; i<= sum; i++){ document.write(sum) } in this case, I want to get two inputs from the user and sum these numbers together Examples: the user enters 5 in the first input the user enters 5 in the second input I want the Result of Sum of them to be in a for loops or while loops I don't know which one is better the result on the screen should be like this 1 2 3 4 5 6 7 8 9 10 as you see we have two inputs (5 and 5) and sum of both inputs should be a variable in for loop if i is < or = sum you should add one number to i variable I hope I've explained my goal

29th Jul 2020, 1:12 AM
coal
coal - avatar
2 Answers
+ 4
var one= prompt("Please enter a number"); var two= prompt("Please enter a number"); var sum = Number(one) + Number(two) for(i = 1; i<= sum; i++){ document.write(i+"<br>") } For you Just try this
29th Jul 2020, 1:16 AM
Ayush Kumar
Ayush Kumar - avatar
0
Why did you put one and two variables in parentheses?
29th Jul 2020, 3:46 AM
coal
coal - avatar