嵌套循环示例

时间:2017-03-31 18:29:20

标签: javascript arrays for-loop

我有一个问题,我必须修改函数 multiplyAll ,以便它将产品变量乘以 arr的子数组中的每个数字。以下是代码段:

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  //Here I have written this but its not working! 
      <!--
       for(product = 1; product < arr.length; product++){
       return arr[product];
      }
      -->
  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);

它在答案中显示[3,4]

我做错了什么?

1 个答案:

答案 0 :(得分:1)

将您的for循环替换为:

for(var i=0; i<arr.length; i++)
{
    for(var j=0; j<arr[i].length; j++)
    {
        product *= arr[i][j] ;
    }
}
相关问题