Can someone please let me know where I went wrong here? When I run the code, I get 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 10. How would I make the loop stop at 0? When I remove the last line of code (console.log(countDown);), the loop ends at 0 but the challenge isn't completed. Any help is greatly appreciated!
Using a for loop, decrement countDown by one each time the loop runs until it equals 0, making use of looping functionality instead of logging each number separately.
let countDown = 10;
// ADD CODE HERE
for (let countDown = 10; countDown>0;countDown--)
{
console.log(countDown);
}
// Uncomment the below line to check your work
console.log(countDown) // -> should print 0;
Problem is that you have two countDown
variables in your code, one at the top and one declared as a loop variable, in the initialization part of the for
loop.
They both are different variables. Your loop is terminated correctly but when you log the countDown
variable after the loop, you are not logging the one used in the loop, you are logging the one declared above the loop.
You don't need to declare countDown
variable in loop, just use the one declared above the loop.
let countDown = 10;
for (; countDown > 0; countDown--) {
console.log(countDown);
}
console.log(countDown) // -> should print 0;