如何使用Javascript显示金字塔? [英] how to Display Pyramid using Javascript?
本文介绍了如何使用Javascript显示金字塔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是显示PYRAMID但不能产生所需输出的代码。
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="";
}
}
这篇关于如何使用Javascript显示金字塔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文