Skip to content

Instantly share code, notes, and snippets.

@yuanchuan
Created March 16, 2011 00:58
Show Gist options
  • Save yuanchuan/871830 to your computer and use it in GitHub Desktop.
Save yuanchuan/871830 to your computer and use it in GitHub Desktop.
rinting 1 to 1000 without loop or conditionals
//Printing 1 to 1000 without loop or conditionals
//http://stackoverflow.com/questions/4568645/printing-1-to-1000-without-loop-or-conditionals/4583502#4583502
// using replace
(function(max){
new Array(max + 1).join(' ').replace(/\s/g, function(c, i){
console.log(i + 1);
})
})(1000)
// using recursion (so ineffective)
(function print(i, max) {
console.log(++i);
({0 : print, 1 : function(){}})[(i / max) >> 0](i, max);
})(0, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment