2

I want the loop to count down starting with the user's input number all the way down to 0. For example, if the user puts in 10, it will count down from 10 to 0. I think I'm close but need a little help.

var userNum = Number(window.prompt("Enter number of your choice starting from 1"));
var i;
for (i = 0; i < userNum; i--) {
    window.console.log(userNum[i]);
}
Aj96
  • 187
  • 8

2 Answers2

2

You should make the loop start at userNum and end at 0:

const userNum = Number(window.prompt("Enter number of your choice starting from 1"));
for (let i = userNum; i>=0 ; i--) {
    console.log(i);
}

Also, if you decrement here, use >= instead of <. userNum[i] doesn't work, it's a number not an iterable, Array-like struct.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • 1
    Thanks! That makes sense now. As I'm sure you can tell, I'm just learning the fundamentals of JS. – Aj96 Feb 20 '19 at 19:59
0

Personally I prefer to use :

const userNum = Number(window.prompt("Enter number of your choice starting from 1"));

for(let x=1+userNum; x-->0;){
  console.log('x',x);
}
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40