why the following code doesn't work? it is going in Illegal invocation exception:
function forEach(array , action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
forEach([1,2,3], console.log);
why the following code doesn't work? it is going in Illegal invocation exception:
function forEach(array , action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
forEach([1,2,3], console.log);
You need to do it like this:
function forEach(array , action) {
for (var i = 0; i < array.length; i++){
action(array[i]);
}
}
You can call the above as:
forEach([1,2,3], function(value){
console.log(value)
});
OR
forEach([1,2,3], console.log.bind(console));