Skip to content

Instantly share code, notes, and snippets.

@bohford
Created August 12, 2010 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohford/521502 to your computer and use it in GitHub Desktop.
Save bohford/521502 to your computer and use it in GitHub Desktop.
// Given the following javascript code:
function countdown (num) {
for (var i = 0; i <= num; i += 1) {
setTimeout(function () {
alert(num - i);
}, i * 1000);
}
}
countdown(5);
// The desired result is a countdown from 5 to 0 using alert messages.
// Explain why the code only alerts -1, then fix the code so it works as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment