I am writing code to try and create a countdown function in the console. I want the code to continue to run 5 times and then end with the phrase "Finished!" When I run this code as is it stops after one time. Am I supposed to add a while function to get it to run 5 consecutive times? Any help would be appreciated. Thanks!
var i = 5;
function startTimer() {
var countdownTimer = setInterval(function() {
console.log(i);
i = i - 1;
if (i <= 0) {
clearInterval(countdownTimer);
console.log("Finished!");
}
}, 1000);
}
startTimer();
If you wanted your countdown to run 5 times you could do it just like this: I´d say the code changes are pretty self explaining
var i = 5;
var k = 5;
function startTimer() {
var countdownTimer = setInterval(function() {
console.log(i);
i = i - 1;
if (i <= 0) {
clearInterval(countdownTimer);
console.log("Finished!");
k = k - 1;
if(k > 0 ){
i = 5;
startTimer();
}
}
}, 1000);
}
startTimer();