Month: March 2014

Sleepsort

Sleepsort might be a joke, but its implementation demonstrates three very important things about javascript: closures, asynchronous functions, and variable hoisting. It is therefore an excellent example for a beginner to study to understand javascript better. The Naive Implementation function sleepsort (input) { for(var i=0; i<input.length; ++i) { setTimeout(function () { console.log(input[i]); }, input[i] * 1000); } }; sleepsort([3,1,2]); Output: undefined undefined undefined What happened […]

Continue Reading