Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display pyramid using JavaScript?

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

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

Required Output

like image 327
nida Avatar asked Dec 23 '13 07:12

nida


1 Answers

Try the below code

function generatePyramid() {
    var totalNumberofRows = 5;
    var output = '';
    for (var i = 1; i <= totalNumberofRows; i++) {
        for (var j = 1; j <= i; j++) {
            output += j + '  ';
        }
        console.log(output);
        output = '';
    }
}

generatePyramid();
   
like image 104
renuka Avatar answered Nov 15 '22 11:11

renuka