Nesting For Loops

I can’t figure out what I’m doing wrong on this challenge:

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

experimental

1 Like

Thanks for the help!

I have problems with this exercise. Can you help me? I think I have followed your instructions but it does not work.

This is my code:

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]; j++) {
console.log([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]]);

1 Like

Hi, if you still need help:
As I can see, in your embedded for loop you don’t use the .length method on your array in the condition part.
You have:
for (var j = 0; j < arr[i]; j++)
but it should be:
for (var j = 0; j < arr[i].length ; j++)
because you want to end the for loop when you reached the end of the inner array.
I hope that helps.

7 Likes

Thank you so so much!! :slight_smile:

Thanks for the help guys. You gave an answer without just giving away all the details. Awesome and super bad a** :rocket:

I am having issues with this task. I looked over what was done here and I don’t see what I have done wrong. Can someone help me out?

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++) {
      console.log([i][j]);
      product = product * multiplyAll[j];
    }
  }
  // Only change code above this line
  return product;
}

// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
console.log([i][j]);

You are missing a variable name here. Check the example.

product = product * multiplyAll[j];

I think you are mixing up the name of your function and the name of the function parameter. Also, you need to use both i and j to access a value in the array, not just j.

1 Like

Why is arr.length resulting in an undefined value?

arr.push(1); is meant to test array behavior which seems to work fine.

Update

Found my mistake :slight_smile:

This is should work:

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

3 Likes

HY! Is console.log() necessary here? i mean some of you used it, but isnt “return” do the same.?
So is it unnecessary/or doesnt metter or maybe wrong…? (its a theoretical question=)

Why don’t you try typing it here and see how console log is different from return.

https://repl.it/languages/javascript

Basically you can only return one thing in an a function. That’s what you’re function does. Adding console log just prints whatever you want to print out.
Feel free to copy past this in repl:

function logVSreturn(returnStatement){
	console.log(returnStatement)
	for (var i = 0; i < 3; i++){
		console.log("log: " + i);
	}
	console.log("You can log anything");
	return returnStatement;
}

logVSreturn("hi")

1 Like

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++) {
console.log(arr[i][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]]);

1 Like

can be shorter

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

return product;
}
multiplyAll([[1,2],[3,4],[5,6,7]]);

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

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

function logVSreturn(returnStatement){
	console.log(returnStatement)
	for (var i = 0; i < 3; i++){
		console.log("log: " + i);
	}
	console.log("You can log anything");
	return returnStatement;
}

Check the complete Bash for loop Tutorial here https://www.puttygen.com/chown-command-in-linux

Can somebody tell me why we use returnStatement instead of logVSreturn?

console.log() prints the output the the console so the user can see it, but the information is not usable by the rest of the code.

return passes the value outside of the function so that it can be used by other pieces of code.

In general, you will want to return instead of console.log() with functions.