Pyramid
Write a function that accepts positive number N. The function should console log a step shape with N levels using the # character. Make sure the step has spaces on the right hand side.
Example:
console.log(pyramid(5))
“ # “
“ ### “
“ ##### “
“ ####### “
“#########”
function pyramid(n) {
const midpoint = Math.floor((2 * n - 1) / 2);
for (let row = 0; row < n; row++) {
let level = '';
for (let column = 0; column < 2 * n - 1; column++) {
if (midpoint - row <= column && midpoint + row >= column) {
level += '#';
} else {
level += ' ';
}
}
console.log(level);
}
}
console.log(pyramid(5))
First we create function in our case pyramid with only one parameter named n, also we create a midpoint variable that will be equal to Math.floor((2n-1/) /2);
We create for loop where *row is 0; row < n; and row++ and we create variable level to empty string.
Inside the first for loop where we loop the rows now we do the same with the columns so we create for loop where column is 0; column < 2 * n - 1; column++
We have if statement where we check if **midpoint* - row <= column && midpoint + row >= column*; And if true level will be concatinated with ‘#’ sign else level will be concatinated with empty string
This if statement right here is going to make sure that the current *column that we are looking at is whitin bounds of midpoint - row and midpoint + row
In the end we console.log(level)