OneBug This week's bugs
Spot the bug JavaScript
1
// Announce each queued job, one per second.
2
var jobs = ["backup", "resize", "email"];
3
for (var i = 0; i < jobs.length; i++) {
4
  setTimeout(function () {
5
    console.log("job " + i + ": " + jobs[i]);
6
  }, (i + 1) * 1000);
7
}
8
// expected: job 0: backup, job 1: resize, job 2: email
9
// actual:   job 3: undefined  (three times)  (!)

Read the full write-up for this bug (spoilers)