我一直在做一些關於freecodecamp的練習,我被困在這個循環練習的嵌套上。我能找到解決方案,但我不太明白。嵌套for循環多維數組。概念
有人可以向我解釋變量J的第二個循環是如何工作的?我已經在網上閱讀說第一個for循環是用於外部數組,第二個是內部數組,但爲什麼停在兩個for循環,爲什麼不是三個?
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0;i<arr.length;i++) {
for (var j=0;j<arr[i].length;j++) {
product *= arr[i][j];
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
因爲'內部數組'的長度= 2 ..並且循環條件小於長度,所以它將迭代索引=> 0 ... 1 ...中斷... – Rayon
您可以使用調試器爲line在每一次迭代中記下「i」和「j」的值,或者我建議你自己用筆或紙做一次。 –
因爲數組嵌套到二級。 – 2016-04-09 11:33:27