How to display pyramid using JavaScript?

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

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

22条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 12:20

    You should generate an array on every row iteration and output it at the end:

    function generatePyramid() {
        var totalNumberofRows = 5,
            arr;
        for (var i = 1; i <= totalNumberofRows; i++) {
            arr = [];
            for (var j = 1; j <= i; j++) {
                arr.push(j);            
            }
            console.log(arr.join(" ") + "\n");
        }
    }
    

提交回复
热议问题