Pyramid

by Alex Myronov

JavaScript

const buildPyramid = (rows) => {
	const cols = rows * 2 - 1
	for (let row = 0; row <  rows; row++) {
    let level = ''
    const filledCells = 2 * row + 1

		const startPosition = (cols - filledCells) / 2
    const endPosition = startPosition + filledCells
    for (let col = 0; col < cols; col++) {
			if (col < startPosition || col >= endPosition) {
        level += '0'
      } else {
        level += '7'
      }
    }
    
    console.log(level)
  }
}

buildPyramid(7)


/* 
(2 * index) + 1 = 1
(2 * index) + 1 = 3
(2 * index) + 1 = 5
(2 * index) + 1 = 7

(cols - 1) / 2 = 4
(cols - 3) / 2 = 3
(cols - 5) / 2 = 2
(cols - 7) / 2 = 1
*/

/* 

000070000 - 0
000777000 - 1 
007777700 - 2
077777770 - 3
777777777 - 4

*/

/* 

0007000 - 0
0077700 - 1 
0777770 - 2
7777777 - 3
*/

/* 

00700
07770
77777

 */

/* 
070
777
*/