Clearing up for loops

Clearing up for loops
0.0 0

#1

Hiya, I managed to get the code for this task to work with a little bit of googling. My problem is I’m not exactly sure what is happening inside the function.
Where im confused is at the conditions inside the for loops. I understand initializing the var but after that Im not sure whats happening to give me the outputs I’m looking for.
If someone could break this down for me into simpler terms it would be appreciated!

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]]);

multiplyAll([[1],[2],[3]]) should return 6
multiplyAll([[1,2],[3,4],[5,6,7]]) should return 5040
multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]]) should return 54

#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

If you log out values to the console you can watch what each step is doing.

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  console.log("arr is ", arr)
  for (var i = 0; i < arr.length; i++){
    for (var j = 0; j < arr[i].length; j++){
      console.log(`i:${i},j:${j}  arr[${i}] is ${arr[i]}  and arr[${i}][${j}] is ${arr[i][j]}`);
      console.log(`product * arr[i][j] is ${product} X ${arr[i][j]} = ${product * arr[i][j]}\n`)
      product *= arr[i][j];
    }
  }
  // Only change code above this line
  return product;
}

You might have to open developers tool in your browser to see this output. If you’re not sure how to do that Google that for your specific browser. does that in Chrome and Firefox

Here, I put on repl.it


#4

So from what I gather:

var i = 0; // this is the value that stores which array in the list I am using
var j = 0; // this is the value that stores which value inside of the array i will be using