Skip to content

Instantly share code, notes, and snippets.

@kanziw
Created December 27, 2018 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanziw/2bf0ac6aa3bf46f485d4d38a899dd55f to your computer and use it in GitHub Desktop.
Save kanziw/2bf0ac6aa3bf46f485d4d38a899dd55f to your computer and use it in GitHub Desktop.
Idea of range in js
function* rangeG(length) {
let cnt = -1
while (cnt < length - 1) yield ++cnt
}
function range(length) {
return [...rangeG(length)]
}
console.log(range(0)) // []
console.log(range(10)) // [0, 1, ..., 9]
console.log('-------------------------')
function range2(length) {
return [...Array(length).keys()]
}
console.log(range2(0)) // []
console.log(range2(10)) // [0, 1, ..., 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment