Created
October 8, 2012 09:08
-
-
Save schatteleyn/3851547 to your computer and use it in GitHub Desktop.
Js Range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Naive implementation of a range in Javascript. I needed that for a project, and coming from Ruby, I was disappointed to find there wasn't that. | |
So I implemented one. | |
b => beginning value of the range | |
e = ending value of the range | |
i => include or not the last value (.. or ... in Ruby) | |
*/ | |
var array = new Array; | |
function range(array, b, e, i) { | |
if (typeof i == 'undefined') { | |
i = true; // if include is not defined, include by default | |
} | |
if (e < b){ | |
if (i == true){ | |
return array; | |
} | |
else { | |
return array.pop(); | |
} | |
} | |
else { | |
array.unshift(e); | |
range(array, b, e - 1, i); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment