Finders Keepers Kata

10 May 2020

Problem Statment

Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument). If no element passes the test, return undefined.

Solution

I was pretty happy with my original solution, then asked for feedback and was shown an even cooler way of doing it! Here's the cool solution:

``````
function findElement(arr, func) {
return arr.find(func);
}

findElement([1, 2, 3, 4], num => num % 2 === 0);
```
```

The find method returns the value of the first element in the provided array that satisfies the provided testing function.

When I first solved this problem, I didn't know that Array.find() existed, so I did it in a few extra steps, as follows:

``````
function findElement(arr, func) {
const test_results = arr.map(e => func(e));

return arr[test_results.indexOf(true)];
}

findElement([1, 2, 3, 4], num => num % 2 === 0);
```
```

In the solution above, I mapped each item in the original array to a new array where each element is either true or false depending on whether the elements in the array met the conditions in the passed-in function.

Then, it returns the first element which meets the true condition.