2

Trying to make this function true. I think the problem is in my for loop.

function forEach(array, callback){    
    console.log(array, callback);
    for(var i = 0; i < array.length; i++) {

    }
}

 // testing your code with console.assert
var total = 1;
var myArray = [1, 2, 3, 4];
function multiplyTotal(a) {
    total *= a;
}
forEach(myArray, multiplyTotal);
// and finally assert; if this fails, the program stops
console.assert(total === 24);
2
  • 5
    You don't actually call you callback? What did you expect to see happen? Add callback(array[i]); in your for loop. Or just use the existing forEach functions in Javascript. Commented Mar 10, 2016 at 20:25
  • Your problem is the empty body of that for loop. You'll need to execute the things that you expect your function to do in there. Commented Mar 10, 2016 at 20:29

2 Answers 2

2
function forEach(array, callback){    
    console.log(array, callback);
    for(var i = 0; i < array.length; i++) {
       callback(array[i]); // you need to call callback function
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Additionally, Javascript already has a built in function for this:

myArray.forEach(multiplyTotal);

http://www.w3schools.com/jsref/jsref_forEach.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.