Bug in Finders Keepers challenge?

Bug in Finders Keepers challenge?
0.0 0

#1

Hi everyone,
I always write my code in Atom because I find it more comfortable to work there are then copy/paste everything into the FCC editor. My solution for Finders Keepers works wonderfully on my locally saved files but doesn’t work in FCC, even though the console is giving me no error and logging expected results. The only quirk is that it’s giving me the first result (2) twice on the FCC page (not on my file).
Does anyone know how to fix this? Unless there’s something wrong with my code that I’m not seeing :thinking: Thanks!!


function findElement(arr, func) {
  var newArr = [];
  newArr.push(arr.filter(func));
  var num = newArr.join("").substr(0,1);
  console.log(num);
  return num;
}

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

#2

Both of the arrays passed to findElement in the tests contain numbers as elements. Your function returns a string instead of what the original value in the array was. You should not be mutating the original values in any way.

The filter function creates a new array of all the elements where the function passed in evalutes to true, so there is no reason to push that result to a another array (newArr). You just need to return the first element of the filtered array. You should already know how to reference the first element of an array.


#3

Yes!! Did it with find instead of filter :grin:
Thanks so much!!


#4

Glad you found a solution. If you would have used filter, you could have written:

function findElement(arr, func) {
  return arr.filter(func)[0];
}