Hi everyone, I am a beginner in javascript and am running into a small issue. In the code snippet below, the desired result is a countdown from 5 to 0 using alert messages. But for some reason it alerts only -1, why is that and how can I fix this issue. Thanks a lot in advance.
function countdown (num) {
for (var i = 0; i <= num; i += 1) {
setTimeout(function () {
alert(num - i);
}, i * 1000);
}
}
countdown(5);