使用数组和嵌套循环制作简单的时间表?

时间:2014-04-06 14:41:19

标签: javascript arrays loops nested

我有这个问题我想为console.log做一个timestable输出,但我是初学者时遇到数组问题。这是我的代码,一直盯着这个。此致。

<script>

// Simple array to store and output times’ tables 
var timesTable = new Array(12); 
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; 
timesTable[5] = 5 * multiplier; 


    for (multiplier=0; multiplier<13; multiplier++){

        console.log("0 x " + multiplier +  " = " + timesTable[12])


for (timesTable=0; timesTable<12; timesTable++){

}} 
</script> 

1 个答案:

答案 0 :(得分:0)

// create an array
var a = [];
var multi = 6;

// fill array with values
for (var i = 0; i < 12; i++) { a.push(i); }

// multiply every array-element with 6 and create new array
var b = a.map(function (j) { return j * multi; })

// log
for (i = 0; i < b.length; i += 1) {
  console.log(i + ' x ' + multi + ' = ' + b[i]);
  // or:
  // console.log(a[i] + ' x ' + multi + ' = ' + b[i]);
}