I tried to create an array in js using for loop but it the loop isn’t stopping at the desired number. can somebody explain why it’s happening. when I am entering number 7 the loop is running upto 70.
this is the code having for loop, after putting index=1 and index>a+1 it doesn’t stop on a+1 condition
the output after I entered 7
Devansh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
The input received is string. The + operator, when used with a string, performs string concatenation instead of addition. So “7” + 1 results in “71”, not 8 .
Just add parseInt()
let a = parseInt(prompt('Enter factorial number'), 10);
let b = [];
for (let index = 0; index <= a; index++) {
b.unshift(index);
console.log(b);
}