# freeCodeCamp Challenge Guide: Finders Keepers

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 8:34pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016 "2016-06-30T20:34:13Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 8:34pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/1 "2016-06-30T20:34:13Z")

</div>

# [Finders Keepers](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers)

* * *

## Problem Explanation

We need to return the element from an array that passes a function. Both the `function` and the `array` are passed into our function `findElement(arr, func)`.

* * *

## Hints

### Hint 1

We need to return the element from an array that passes a function. Both the `function` and the `array` are passed into our function `findElement(arr, func)`. Looking through the array can be done with a `for` loop.

### Hint 2

`num` is passed to the function. We will need to set it to the elements we want to check with the function.

### Hint 3

Do not forget, if none of the numbers in the array pass the test, it should return `undefined`.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function findElement(arr, func) {
> let num = 0;
> 
> for (let i = 0; i < arr.length; i++) {
> num = arr[i];
> if (func(num)) {
> return num;
> }
> }
> 
> return undefined;
> }
> 
> ```
> 
> #### Code Explanation
> 
> - Challenge asks us to look through array. This is done using a `for` loop.
> - The `num` variable is being passed into the function, so we set it to each index in our array.
> - The pre-defined function already checks each number for us, so if it is “true”, we return that num.
> - If none of the numbers in the array pass the function’s test, we return undefined.

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function findElement(arr, func) {
> return arr.find(func);
> }
> 
> ```
> 
> #### Relevant Links
> 
> - [Array.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

> **Solution 3 (Click to Show/Hide)**
>
> \*\* Recursive Solution\*\*
> 
> ```javascript
> function findElement(arr, func) {
> if (arr.length > 0 && !func(arr[0])) {
> return findElement(arr.slice(1), func);
> } else {
> return arr[0];
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 10, 2016, 6:38pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/2 "2016-08-10T18:38:58Z")

</div>

Closed on August 10, 2016, 6:38pm UTC



---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 20, 2016, 8:28pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/3 "2016-08-20T20:28:00Z")

</div>

Opened on August 20, 2016, 8:28pm UTC



---

<div class="post-metadata">

**Author:** ![simonhrogers](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@simonhrogers](https://forum.freecodecamp.org/u/simonhrogers)\
**Post date:** [January 7, 2017, 2:06pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/4 "2017-01-07T14:06:18Z")

</div>

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

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

```

You can just use a find array helper method instead of the filter; makes your code even smaller

---

<div class="post-metadata">

**Author:** ![frontender007](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/frontender007/32/98938_2.png) [@frontender007](https://forum.freecodecamp.org/u/frontender007)\
**Post date:** [January 22, 2017, 11:59pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/5 "2017-01-22T23:59:47Z")

</div>

This could be simple one liner solution which is more modular and obviously less code. Here is how:

```auto

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

And that’s about it. 🙂

---

<div class="post-metadata">

**Author:** ![pennyJack](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pennyjack/32/39751_2.png) [@pennyJack](https://forum.freecodecamp.org/u/pennyJack)\
**Post date:** [February 10, 2017, 9:41am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/6 "2017-02-10T09:41:48Z")

</div>

Or alternatively:

```javascript
function findElement(arr, func) {
  return arr.filter(func).shift();
}

```

---

<div class="post-metadata">

**Author:** ![DaltonK54](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@DaltonK54](https://forum.freecodecamp.org/u/DaltonK54)\
**Post date:** [March 15, 2017, 8:27am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/7 "2017-03-15T08:27:15Z")

</div>

function findElement(arr, func) {

for (var i=0;i\<arr.length;i++){ // loop through the provided array.  
if (func(arr[i])) { // if the provided function is called & passed the parameter of the loops current element & returns true,  
return arr[i]; // return the current loop element to the console.  
}  
}  
return undefined; // if no element of the provided array passes the functions test return undefined.  
}

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

---

<div class="post-metadata">

**Author:** ![Doko85](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@Doko85](https://forum.freecodecamp.org/u/Doko85)\
**Post date:** [March 25, 2017, 12:48am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/8 "2017-03-25T00:48:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![irakligeek](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/irakligeek/32/50010_2.png) [@irakligeek](https://forum.freecodecamp.org/u/irakligeek)\
**Post date:** [June 7, 2017, 12:33am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/9 "2017-06-07T00:33:54Z")

</div>

Hi all,

Here is my code:

```
function find`Eleme`nt(arr, func) {
          //decalre an array to store all value that pass the test
          var arr2 = [];
      
      // add all values that pass the test on func
      arr2 = ( arr.filter(function(el){
        return func(el) ? arr2.push(el) : "";
      }) );
      
      //return first element that passed the test, if it didn't pass the test 
      //it will return undefined - exactly what we need!
      return arr2[0];
    } 
    //test
    findElement([1, 3, 5, 9], function(num){ return num % 2 === 0; });
```

---

<div class="post-metadata">

**Author:** ![ihavenoidea14](https://avatars.discourse-cdn.com/v4/letter/i/e47774/32.png) [@ihavenoidea14](https://forum.freecodecamp.org/u/ihavenoidea14)\
**Post date:** [June 18, 2017, 5:36pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/10 "2017-06-18T17:36:42Z")

</div>

My solution as well!

---

<div class="post-metadata">

**Author:** ![iamatikant](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/iamatikant/32/60095_2.png) [@iamatikant](https://forum.freecodecamp.org/u/iamatikant)\
**Post date:** [July 9, 2017, 11:12am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/11 "2017-07-09T11:12:13Z")

</div>

One more way. A must try for the one, who want to keep it simple:

```auto
function findElement(arr, func) {
  for(var i = 0; i < arr.length; i++) {
    if(func(arr[i])) {
      return arr[i];
    }
  }
  return undefined;
}

```

---

<div class="post-metadata">

**Author:** ![coderdix](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/coderdix/32/54407_2.png) [@coderdix](https://forum.freecodecamp.org/u/coderdix)\
**Post date:** [July 9, 2017, 8:41pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/12 "2017-07-09T20:41:03Z")

</div>

Looking at other solutions, I could have just chained the shift method to the filter.

```
function findElement(arr, func) {
  
  let test = arr.filter(func),
      output = test.shift();
  
  return output
}

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

---

<div class="post-metadata">

**Author:** ![String\_BD](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@String\_BD](https://forum.freecodecamp.org/u/String_BD)\
**Post date:** [July 20, 2017, 5:30am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/13 "2017-07-20T05:30:48Z")

</div>

Did it in the intermediate way in just one line!!!

```auto
function find(arr, func) {
  return arr.filter(func)[0];
}

```

---

<div class="post-metadata">

**Author:** ![0c0de](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/0c0de/32/73096_2.png) [@0c0de](https://forum.freecodecamp.org/u/0c0de)\
**Post date:** [July 23, 2017, 7:09pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/14 "2017-07-23T19:09:47Z")

</div>

This is my solution, for this problem, it was easy to find the solution for this anyways is a challenge:

```
function findElement(arr, func) {
  var num = 0;
  for(j = 0; j < arr.length; j++){
    if(func(arr[j]) === true){
      return arr[j];
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![rodrigodiego](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rodrigodiego/32/59117_2.png) [@rodrigodiego](https://forum.freecodecamp.org/u/rodrigodiego)\
**Post date:** [July 26, 2017, 1:46pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/15 "2017-07-26T13:46:40Z")

</div>

function findElement(arr, func) {

var num = arr.filter(function(a){  
return func(a);  
});  
return num[0];  
}

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

---

<div class="post-metadata">

**Author:** ![ENGMENJARI](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/engmenjari/32/83475_2.png) [@ENGMENJARI](https://forum.freecodecamp.org/u/ENGMENJARI)\
**Post date:** [August 1, 2017, 2:45pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/16 "2017-08-01T14:45:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Madness2aMaze](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/madness2amaze/32/80309_2.png) [@Madness2aMaze](https://forum.freecodecamp.org/u/Madness2aMaze)\
**Post date:** [August 1, 2017, 3:01pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/17 "2017-08-01T15:01:00Z")

</div>

Hello,

Just keeping it simple:

```auto
    function findElement(arr, func) {
      
      for (var i = 0; i < arr.length; i++)
        if(func(arr[i])) return arr[i];
    }
    //You make this look easy!
    findElement([1, 3, 5, 8, 9, 10], function(num){ return num % 2 === 0; });

```

---

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 10, 2019, 1:22am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/19 "2019-08-10T01:22:29Z")

</div>

Opened on August 10, 2019, 1:22am UTC



---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 4:03pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-finders-keepers/16016/20 "2025-01-08T16:03:00Z")

</div>

Closed on January 8, 2025, 4:03pm UTC


