DEV Community

Discussion on: Daily Challenge #52 - Building a Pyramid

Collapse
 
ynndvn profile image
La blatte • Edited

Here it is!

// Minified
f=(a)=>[...Array(a)].map((_,i)=>(c=' '.repeat(a-i),`${c}${'*'.repeat(2*i+1)}${c}`)).join`\n`

// Readable
const pyramid = (input) => {
  return [...Array(input)].map((_, index) => {
    const padding = ' '.repeat(input - index);
    const stars = '*'.repeat(2 * index + 1);
    return `${padding}${stars}${padding}`;
  }).join('\n');
}

The right padding isn't necessary, but hey why not

'\n' + pyramid(10)
"
          *          
         ***         
        *****        
       *******       
      *********      
     ***********     
    *************    
   ***************   
  *****************  
 ******************* "