Basic Algorithm Scripting: Finders Keepers

Basic Algorithm Scripting: Finders Keepers
0.0 0


So, I know how to solve this one with a for loop. And I read this post (freeCodeCamp Algorithm Challenge Guide: Finders Keepers) so now I know how to solve it using .filter().

I tend to reach for .map() a lot when I would have used a for loop before, but I couldn’t get this working using .map().

This is more or less what I was trying:

function findElement(arr, func) {
  return => {
      return num

Can anyone explain where I went wrong? Thanks.


That makes sense. The way I was thinking of it was that num would only be returned if func evaluated to true, so it would work the same way that using .filter() would, and you would return newArr[0].


What was his answer? I also tried to solve it exactly the same way using map…


There are ways to use map() in this challenge, but not without writing more code than necessary. This is a job for filter().

Take a look at the following minimalist solution. If it makes little sense, try to rewrite it more verbosely. It uses arrow syntax, which allows you to omit curly brackets and the words function and return .

let findElement = (arr, func) => arr.filter(func)[0];