Javascript 例如,循环的嵌套
我有一个问题,我必须修改函数multiplyAll,以便它将乘积变量乘以arr子数组中的每个数字。以下是代码片段:Javascript 例如,循环的嵌套,javascript,arrays,for-loop,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.l
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]]);
函数多路传输(arr){
var乘积=1;
//仅更改此行下方的代码
//在这里,我写了这个,但它不工作!
//仅更改此行上方的代码
退货产品;
}
//修改下面的值以测试代码
多音节([1,2],[3,4],[5,6,7]);
它在答案中显示[3,4]
我做错了什么?将您的
for
循环替换为:for(var i=0; i<arr.length; i++)
{
for(var j=0; j<arr[i].length; j++)
{
product *= arr[i][j] ;
}
}
for(var i=0;i你想用什么乘以什么?你期望得到什么样的结果?你想要像[2,12,210]?multiplyl([1],[2],[3]);应该返回6个multiplyl([1,2],[3,4],[5,6,7])应该返回5040个multiplyll([5,1],[0.2,4,0.5],[3,9]);)应该返回54