Basic Algorithm Scripting: Finders Keepers Issue

Hello there.

Could someone please tell me why FCC is not accepting my solution, even though it passes all the tests?

function findElement(arr, func) {
    "use strict"
    return arr.filter(num => func(num)).toString ('').substring(1,0);
  console.log(findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0));
 // It returns 8 as expected...

Please feel free to paste this code on an editor and test it out.

Thanks in advance!

You’re getting the correct result. Then you’re converting that correct result to a string, then only taking the first character of that string. So your solution will always be incorrect except when you are using the function on an array of single-character strings.


I have solved it like this:

function findElement(arr, func) {
    "use strict"
   let [a] = arr.filter(num => func(num));
   return a;

Thanks for the reply. I have just got it straight here

1 Like