I wrote a function that when called returns just 5, yet when I console.log the function inside of the setInterval() it returns exactly what I expect. So why doesn't it work when called?
function countdown(seconds){
var tick = 0;
setInterval(function() {
if (seconds > tick) {
seconds--;
console.log(seconds); //counts down as expected.
}
}, 1000);
return seconds;
}
console.log(countdown(5)); //returns undefined.
Also, I know setTimeout() is preferred but it when I swap out setInterval it doesn't work at all. Any help and clarity with this issue would be greatly appreciated it.