How to display pyramid using JavaScript?

前端 未结 22 874
长情又很酷
长情又很酷 2020-11-29 11:45

Here is the code to display pyramid but its not exactly producing required output.

22条回答
  •  伪装坚强ぢ
    2020-11-29 12:12

    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);
      }
    

    }

    pyramid(5);

提交回复
热议问题