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]]);’
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]]);
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.
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]]);
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.
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]]);
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=)
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;
}
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]]);
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];
}
}
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 logVSreturn(returnStatement){
console.log(returnStatement)
for (var i = 0; i < 3; i++){
console.log("log: " + i);
}
console.log("You can log anything");
return returnStatement;
}