Nesting For Loops i am confused

Tell us what’s happening:
please i need explanation of my code and why it isn’t passing the test

Your code so far


function multiplyAll(arr) {
  var product = 2;
  // Only change code below this line
  for (var i = 0; i< arr.length; i++){ 
    for (var j = 0; j <arr[i].length; j++)
     console.log( product * arr[i]);
  } 
  // Only change code above this line
  return product;

}
// Modify values below to test your code
multiplyAll([[5,1],[0.2,4,0.5],[3,9]]);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops/

So there are a couple of things going on:

  1. On your second for loop (with j), you forgot to start your code block {}

  2. Your multiplication of product is missing your j loop entirely (take a look back the example they gave in the challenge for a hint)

  3. You are also not ever updating your product variable - hint: use an assignment operator

Hope those help, if you need more hints, let us know. :slight_smile:

i would be happy if you explain based on my code

here is the number 1 problem you wrote
for (var j = 0; j <arr[i].length; j++){
console.log( product * arr[i]);
}
}

Sorry if it wasn’t clear, I was actually trying to explain based on your code.

function multiplyAll(arr) {
  var product = 2;
  // Only change code below this line
  for (var i = 0; i< arr.length; i++){ 
    for (var j = 0; j <arr[i].length; j++) //Issue 1 can be found here: you never opened your code block for this loop
     console.log( product * arr[i]); //Issue 2 & 3 can be found here
  } 
  // Only change code above this line
  return product;

}
// Modify values below to test your code
multiplyAll([[5,1],[0.2,4,0.5],[3,9]]);

So your code currently looks like this:

for (var i = 0; i< arr.length; i++){ 
    for (var j = 0; j <arr[i].length; j++)
     //code goes here
  }

But your 2nd for loop should look something like this:

for (var i = 0; i< arr.length; i++){ 
    for (var j = 0; j <arr[i].length; j++){
     //code goes here
  }
}

It’s a very small difference, but very important.

FYI, C like syntax (like javascript) don’t require curly braces for one line statements.

for (var i = 0; i< 5; i++)
  console.log(i)

if(1) console.log('hello')

But points 2 and 3 still stand…

1 Like

I never knew that, totally good to know :+1:t4:

i think that should solve number 2 problem

function multiplyAll(arr) {
  var product = 2;
  // Only change code below this line
  for (var i = 0; i< arr.length; i++){ 
    for (var j = 0; j <arr[i].length; j++){
     console.log(arr[i][j]);
    }
  } 
  // Only change code above this line
  return product;

}
// Modify values below to test your code
multiplyAll([[5,1],[0.2,4,0.5],[3,9]]);


1 Like

It sure does! That’s prefect for number 2.

i kind of need more hint on point 4

Now your issue is that the product variable is never changed.

1 Like
function multiplyAll(arr) {
  var product = 2; //So product is 2 at this point

  // Only change code below this line
  for (var i = 0; i< arr.length; i++){ 
    for (var j = 0; j <arr[i].length; j++) {
     console.log( product * arr[I][j]);  //HINT: this line needs to be updated
    }
  } 
  // Only change code above this line

  return product; //product is still 2 at this point - so at some point before this return statment is reached, you need to update the variable product. An assignment operator would look like product = something

}
// Modify values below to test your code
multiplyAll([[5,1],[0.2,4,0.5],[3,9]]);