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 Answers 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
}
}
answered Mar 10, 2016 at 20:25
madox2
52.3k21 gold badges106 silver badges101 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Additionally, Javascript already has a built in function for this:
myArray.forEach(multiplyTotal);
Comments
lang-js
callback? What did you expect to see happen? Addcallback(array[i]);in yourforloop. Or just use the existingforEachfunctions in Javascript.