I have to create a function which takes an array as argument, with a forEach loop which console.log each element and each index for every iteration inside the function. Also inside the function declare a variable called count, and increment it by one for each iteration then return count.
//====================== EXAMPLE ========================
looper([2,4,8,7])
4 // <====== EXPECTED OUTPUT
//=========================================================
I wrote this function:
function looper(arr) {
arr.forEach(function console (item, index){
var count = 0;
count++;
console.log(("I am item ", item, "I am the index ", index));
return count;
})
}
But I get the following error:
VM76:5 Uncaught TypeError: console.log is not a function
at console (<anonymous>:5:17)
at Array.forEach (<anonymous>)
at looper (<anonymous>:2:9)
at <anonymous>:1:1
How is it that console.log is not a function? Isn't it prebuilt into every browser?
forEach()has no effect, it always returnsundefinedper its definition..logdoes not exist on it also your return statement has to be outside of theforEachloop to return the valueconsoleas name's function. replace to :arr.forEach(function(item, index){