Created
February 25, 2019 09:59
-
-
Save Sparkenstein/f5b3f4c06484be58ba08bce90fb1b536 to your computer and use it in GitHub Desktop.
python like range genrator function in js
This file contains hidden or 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
const range = function* (from, to, step = 1) { | |
let i = from; | |
if (from < to) { | |
while (i + step <= to) { | |
yield i = i + step; | |
} | |
} else { | |
while (i >= to + step) { | |
yield i = i - step; | |
} | |
} | |
} | |
const r = [...range(20, 10, 02)] | |
console.log(r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment