DEV Community

loading...

Discussion on: Daily Challenge #40 - Counting Sheep

Collapse
ynndvn profile image
La blatte

Did anybody talk about oneliner?

f=(i)=>[...Array(i)].map((_,i)=>`${i+1} sheep... `).join``

And the results

f(10);
// "1 sheep... 2 sheep... 3 sheep... 4 sheep... 5 sheep... 6 sheep... 7 sheep... 8 sheep... 9 sheep... 10 sheep... "
Collapse
andymardell profile image
Andy Mardell • Edited

This is sweet. Something similar but with more Array and less map:

const f = i => Array.from(Array(i), (_, i) => `${i + 1} sheep... `).join``

or

const f = i => Array.from({ length: i }, (_, i) => `${i + 1} sheep... `).join``