Finders Keeper Algorithm help

Here is the code:

function findElement(arr, func) {
  var num = 0;

  
  num = arr.filter(func);
  num.splice(1);
  
  num = num.join(" ");
 
  return num;
} 

And well it returns the right number, but I am not passing the challenge.

For example: findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; });

^^ That returns 8, which is what it is supposed to return. But I am not getting the green checkmark that says I passed that test.

Not sure why it doesn’t work, but instead of join you can better use num[0] this will also automatically return undefined if there is no element (second test):

function findElement(arr, func) {
  var num = 0;
  
  num = arr.filter(func);
  num.splice(1);
  
  return num[0];
}
1 Like

It doesn’t work because .join() returns string.

1 Like

Yeah that fixed it, thanks