I'd suggest the best way to do range today would be:
const range = (start, end)=>Array.from({length: end-start}, (__,i)=>start+i)
No need to double memory and processing by spreading the result into an array or making another one with map.
Yup, that is covered in the many other ways of doing this link.
For most purposes, an iterator is actually preferable. These can be created via generators, as mentioned in the article. I wrote one in an older article of mine: dev.to/emnudge/generating-arrays-i...
class Range { constructor(min, max, step = 1) { this.val = min; this.end = max; this.step = step; } * [Symbol.iterator]() { while (this.val <= this.end) { yield this.val; this.val += this.step; } } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'd suggest the best way to do range today would be:
No need to double memory and processing by spreading the result into an array or making another one with map.
Yup, that is covered in the many other ways of doing this link.
For most purposes, an iterator is actually preferable. These can be created via generators, as mentioned in the article.
I wrote one in an older article of mine:
dev.to/emnudge/generating-arrays-i...