Skip to content

Instantly share code, notes, and snippets.

@Pushplaybang
Created April 25, 2015 21:39
Show Gist options
  • Save Pushplaybang/187a5a92663d1d98834d to your computer and use it in GitHub Desktop.
Save Pushplaybang/187a5a92663d1d98834d to your computer and use it in GitHub Desktop.
Javascript range function
var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;
if (step === 0) {
throw TypeError("Step cannot be zero.");
}
if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}
typeof step == "undefined" && (step = 1);
if (end < start) {
step = -step;
}
if (typeofStart == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
} else if (typeofStart == "string") {
if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}
start = start.charCodeAt(0);
end = end.charCodeAt(0);
while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}
} else {
throw TypeError("Only string and number types are supported");
}
return range;
}
// care of alex on SO http://stackoverflow.com/a/1470494/1424399
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment