Nesting For Loops

Nesting For Loops
0

#1

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

experimental


#3

Thanks for the help!


#4

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


#5

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.


#6

Thank you so so much!! :slight_smile:


#7

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


#8

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

#9
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.


#10

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:


#12

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


#13

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=)


#14

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")


#15

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


#16

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