我正在尝试在 javascript 中使用 for 循环来创建一个显示 * 的 table 并且对于每一行,列增加 1 *
I am trying to use a for loop in javascript to create a a table that displays * and for each row the columns increase by 1 *
***let numRows = 5;
let numColumns = 10;
let strRowOutput = "";
for (let row = 1; row <= numRows; row++) {
for (let column = 1; column <= numColumns; column++) {
strRowOutput += "*";
}
console.log(strRowOutput);
strRowOutput = "";
}
console.log();***
这就是到目前为止的代码,但它只显示带有 10* 的最后一行。enter image description here
第二个 for 循环条件应该依赖于第一个 for 循环中的变量,例如 column <= row
或者如果要将列限制为最大值,可以使用 column <= Math.min(row, numColumns)
我在这里使用第二种方法
工作fiddle
let numRows = 5;
let numColumns = 10;
let strRowOutput = "";
for (let row = 1; row <= numRows; row++) {
for (let column = 1; column <= Math.min(row, numColumns); column++) {
strRowOutput += "*";
}
console.log(strRowOutput);
strRowOutput = "";
}
console.log();
***let numRows = 5;
let numColumns = 10;
let strRowOutput = "";
for (let row = 1; row <= numRows; row++) {
for (let column = 1; column <= numColumns; column++) {
strRowOutput += "*";
}
console.log(strRowOutput);
strRowOutput = "";
}
console.log();***
这就是到目前为止的代码,但它只显示带有 10* 的最后一行。enter image description here
第二个 for 循环条件应该依赖于第一个 for 循环中的变量,例如 column <= row
或者如果要将列限制为最大值,可以使用 column <= Math.min(row, numColumns)
我在这里使用第二种方法
工作fiddle
let numRows = 5;
let numColumns = 10;
let strRowOutput = "";
for (let row = 1; row <= numRows; row++) {
for (let column = 1; column <= Math.min(row, numColumns); column++) {
strRowOutput += "*";
}
console.log(strRowOutput);
strRowOutput = "";
}
console.log();