DEV Community

Discussion on: Daily Challenge #201 - Complete the Pattern

Collapse
 
ynndvn profile image
La blatte • Edited

Well, here we go for some JS oneliner:

p=i=>[...Array(0|i)].reduce((a,_,l)=>[...a,Array(l+1).fill(l+1).join``],[]).join`\n`

It works like this :

  • First, build an array with a 0|i length (it will floor the number)
  • Use the reduce method to loop through the built array. Use the a parameter, which is the response array (initialized as [] and updated at each iteration), and the l parameter which is the current index.
  • For each iteration, add to the a array an item which is described as follow : Build a l+1-long array, fill it with l+1 (hence, if we are at l=1, the built item will be equal to [2, 2]). Then, use the join method to convert it to a string ('22'in this case)
  • Finally, once the array is built, use the join method one last time to add line breaks between lines