Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created August 2, 2017 16:36
Show Gist options
  • Select an option

  • Save davidbalbert/44625268c76a684589a0c99650edb5b5 to your computer and use it in GitHub Desktop.

Select an option

Save davidbalbert/44625268c76a684589a0c99650edb5b5 to your computer and use it in GitHub Desktop.
function range(start, stop, step = 1) {
if (Math.sign(stop-start) !== Math.sign(step) || step === 0) {
return [];
}
let nums = [];
if (step > 0) {
for (let i = start; i <= stop; i += step) {
nums.push(i);
}
} else {
for (let i = start; i >= stop; i += step) {
nums.push(i);
}
}
return nums;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment