* fix: restructure certifications guide articles * fix: added 3 dashes line before prob expl * fix: added 3 dashes line before hints * fix: added 3 dashes line before solutions
1.8 KiB
1.8 KiB
title
title |
---|
Nesting For Loops |
Nesting For Loops
Problem Explanation
Relevant Links
- Nest One Array Within Another Array
- Iterate Through An Array With A For Loop
- Accessing Nested Arrays
Hints
Hint 1
Make sure to check with length
and not the overall array.
Hint 2
Use both i
and j
when multiplying the product.
Hint 3
Remember to use arr[i]
when you multiply the sub-arrays with the product
variable.
Solutions
Solution 1 (Click to Show/Hide)
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 = 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]]);
Code Explanation
- We check the length of
arr
in thei
for loop and thearr[i]
length in thej
for loop. - We multiply the
product
variable by itself because it equals 1, and then multiply it by the sub-arrays. - The two sub-arrays to multiply are
arr[i]
andj
.