loading...

re: Daily Challenge #194 - Spread Number VIEW POST

FULL DISCUSSION
 

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!

 

Thanks. I didnt notice its zero based

 

Another way:

const spreadNumber = n => Array.from({length:n}).map((_, i) => i+1);
 

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

Code of Conduct Report abuse