Created
December 27, 2018 02:26
-
-
Save kanziw/2bf0ac6aa3bf46f485d4d38a899dd55f to your computer and use it in GitHub Desktop.
Idea of range in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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