How to display pyramid using JavaScript?

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

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

22条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 12:27

    This will create a proper pyramid in a console:

    function createPyramid(rows)
    {
        for (let i = 0; i < rows; i++) {
            var output = '';
            for (let j =0; j < rows - i; j++) output += ' ';
            for (let k = 0; k <= i; k++) output += '* ';
            console.log(output);  
        } 
    }
    
    createPyramid(5) // pass number as row of pyramid you want.

提交回复
热议问题