我是js的新手,但非常渴望学习。我想做2"因为"循环。 1.将6次表的值存储到数组(timesTable),最多12x6。 2.将这些存储的值输出到控制台(例如,0 x 6 = 0,1 x 6 = 0,)。非常感谢。
<script>
var timesTable = new Array();
var multiplier = 6;
timesTable[0] = 0 * multiplier;
timesTable[1] = 1 * multiplier;
timesTable[2] = 2 * multiplier;
timesTable[3] = 3 * multiplier;
timesTable[4] = 4 * multiplier;
timesTable[5] = 5 * multiplier;
console.log("0 x " + multiplier + " = " + timesTable[0]);
console.log("1 x " + multiplier + " = " + timesTable[1]);
console.log("2 x " + multiplier + " = " + timesTable[2]);
console.log("3 x " + multiplier + " = " + timesTable[3]);
console.log("4 x " + multiplier + " = " + timesTable[4]);
console.log("5 x " + multiplier + " = " + timesTable[5]);
</script>
答案 0 :(得分:0)
不需要2个循环,您可以在一个循环中执行此操作:
var upTo = 12;
for (var i=0; i<upTo; i++) {
timesTable.push(i*6)
console.log(i + " x " + multiplier + " = " + i*6);
}
但如果你真的需要2:
var upTo = 12;
for (var i=0; i<upTo; i++) {
timesTable.push(i*6)
}
for (var i=0; i<upTo; i++) {
console.log(i + " x " + multiplier + " = " + timesTable[i]);
}
答案 1 :(得分:0)
您可以在一个for循环中轻松完成,而无需将数组存储在其中。
var multiplier = 6;
for (var i = 0; i < 13; i++) {
console.log(i + ' x ' + multiplier + ' = ' + (i * multiplier));
}
这将打印6次表格至12 https://jsfiddle.net/o3k6feua/