Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* 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
2019-07-24 13:29:27 +05:30

1.8 KiB

title
title
Nesting For Loops

Nesting For Loops


Problem Explanation


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 the i for loop and the arr[i] length in the j 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] and j.