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");
}
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With