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");
}
}
Simple code of Number Pyramid
I would stick to recursive approach in such a case:
Assuming you want to return numbers and not asterisks as the other answers show, here is that solution:
Note that this solution runs in linear (O(n)) time complexity and doesn't sacrifice performance by logging every line to the console, but the entire pyramid at once instead.
Log to the console as such:
console.log(generatePyramid(n));
If you're looking to just draw a triangle as the picture in your question shows, this function will do that (again, in linear time complexity):
Output
Try the below code