Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Last active September 17, 2019 04:26
Show Gist options
  • Save hjzheng/c94f14b5c883c7928dac7ae5748695aa to your computer and use it in GitHub Desktop.
Save hjzheng/c94f14b5c883c7928dac7ae5748695aa to your computer and use it in GitHub Desktop.
range
function range(start, end, step) {
if (arguments.length === 0) {
throw new Error('range fuction expect at least 1 argument')
}
if (arguments.length === 1) {
end = start
start = 0
step = 1
}
if (arguments.length === 2) {
step = 1
}
if (arguments.length === 3) {
if (step === 0) throw new Error('step can not be 0')
}
return Array.from({
length: Math.abs(Math.ceil((end - start) / step))
}, (_,index) => index*step + start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment