Basic JavaScript: Nesting For Loops

Basic JavaScript: Nesting For Loops
0

#1

If you have a multi-dimensional array, you can use the same logic as the prior waypoint to loop through both the array and any sub-arrays. Here is an example:

    var arr = [
      [1,2], [3,4], [5,6]
    ];
    for (var i=0; i < arr.length; i++) {
      for (var j=0; j < arr[i].length; j++) {
        console.log(arr[i][j]);
      }
    }

This outputs each sub-element in arr one at a time. Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.

Modify function multiplyAll so that it multiplies the product variable by each number in the sub-arrays of arr

I’m wondering why my code below doesn’t work. I am having trouble understanding the differences between a parameter and a variable. Why do some people have the correct code with just replaying multiplyAll in the code below with “arr”. I am not too sure how that helps, since isn’t that a parameter which tells the user that there is going to be something included after the variable “multiplyAll”?

Thank you in advance.
Any help would be greatly appreciated.

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  for (var i = 0; i < multiplyAll.length){
    for (var j = 0; j < multiplyAll[i].length){
      product *= multiplyAll[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

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

A function parameter is basically a placeholder for a value which will be passed into the function. We speak of parameters in the function definition. In your function multiplyAll, you have one parameter named arr. When a value is passed into the function, that value is referred to as an argument. An argument is the value passed, but the arr is what you will use to reference this value inside your function.

You can think of arr as a variable local to the function.

The reason your code does not work, is because multipleAll is the function and not the array that is passed into the function. Remember from what I said above, arr represents the array passed in. When you write multiplyAll.length, you are accessing the length of the function (which in JavaScript all functions are technically objects, but I will not go in detail about that here). It just so happens, that the length property of a function is the number of arguments the function expects. We only have one parameter, so the function’s length is 1. What this means, is that your outer for loop will at most attempt one iteration.

However, you have another issue in the outer and inner for loops. For loops have three sections separated by semi-colons. I will refer you to the documentation for what they are. The last two are optional, but you still must have at least 2 semi-colons present inside the ( ) or it will error out. You are missing a semi-colon after the .length in both loops.

Anyway, to remedy your code, you need to replace those 3 internal multiplyAll function reference just arr, so that you will be iterating through the arr. Also, you will need to add something for the 3rd section of your for loop (called the final expression) to increment i in the outer for loop and j in the inner for loop.


#4

Thank you! Your explanations are extremely helpful and cleared up a lot of my misconceptions.
Enjoy the rest of your day!