Skip to content

Instantly share code, notes, and snippets.

@mcorrigan
Created September 12, 2014 23:30
Show Gist options
  • Save mcorrigan/e15075b7bfd3bf95426d to your computer and use it in GitHub Desktop.
Save mcorrigan/e15075b7bfd3bf95426d to your computer and use it in GitHub Desktop.
JavaScript Range Function
// something comparable to PHP range method. Supports numbers and letters.
function range(start, end, step) {
start || alert('must have 1+ params');
if (!end) {end = start; start = 0;}
var step = step || 1;
var range = [];
var is_num = !isNaN(start);
start = !is_num ? start.charCodeAt(0) : start;
end = !is_num ? end.charCodeAt(0) : end;
step = !is_num ? Math.max(Math.round(step), 1) : step;
step = start > end ? step * -1 : step;
while (start != end + step){
range.push(is_num ? start : String.fromCharCode(start));
start += step;
if ((step < 0 && start < end) || (step > 0 && end < start)) {
break;
}
}
return range;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment