Finders Keepers - Code works, test not passing

Tell us what’s happening:

There seems to be an error with the test. It is passing in the console, but the lesson test isn’t passing for some reason. Any ideas?

Your code so far


function findElement(arr, func) {
  
  arr.forEach(function(e){
    
    if (func(e) === true){
      console.log('this element passes the test: ' + e)
      return e;
    }
  });

  return undefined;
}

findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers/

The problem is the callback of the forEach is just returning to the forEach. It does not return anything back to the calling of the findElement function. Try using the Array.prototype.find method. It will accomplish what you are wanting to do. Just make sure you return the result of calling the method on arr.