JavaScript:
const spreadNumber = n => [...Array(n).keys()]
I think you have an off-by-one error: spreadNumber(2) gives [0, 1] instead of [1, 2]. Nice and concise, though!
spreadNumber(2)
[0, 1]
[1, 2]
Thanks. I didnt notice its zero based
That creates an Array from 0 to n not from 1 to n ;-)
const spreadNumber = n => Array(n).fill(0).map((_, i) => i+1);
Yeah, i didnt notice its not zero based
Another way:
const spreadNumber = n => Array.from({length:n}).map((_, i) => i+1);
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.
JavaScript:
I think you have an off-by-one error:
spreadNumber(2)
gives[0, 1]
instead of[1, 2]
. Nice and concise, though!Thanks. I didnt notice its zero based
That creates an Array from 0 to n not from 1 to n ;-)
const spreadNumber = n => Array(n).fill(0).map((_, i) => i+1);
Yeah, i didnt notice its not zero based
Another way: