I already know the original solution to this. It is:
function findElement(arr, func) {
for (var i = 0 ; i< arr.length ; i++) {
if (func(arr[i])) {
return arr[i];
}
};
return undefined;
}
However, what I am trying to do is re-attempting the challenges using a .forEach method instead as a means to practise using .forEach
Can someone guide me with why the below use of .forEach is not passing the test? Thanks
Your code so far
function findElement(arr, func) {
arr.forEach(function(element) {
if (func(element)) {
return element;
}
})
return undefined;
}
findElement([1, 2, 3, 4], num => num % 2 === 0);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers