# Finders Keepers - why doesn't my code work?

**URL:** <https://forum.freecodecamp.org/t/finders-keepers-why-doesnt-my-code-work/283875>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 13, 2019, 9:42pm UTC](https://forum.freecodecamp.org/t/finders-keepers-why-doesnt-my-code-work/283875 "2019-06-13T21:42:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![colfah](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@colfah](https://forum.freecodecamp.org/u/colfah)\
**Post date:** [June 13, 2019, 9:42pm UTC](https://forum.freecodecamp.org/t/finders-keepers-why-doesnt-my-code-work/283875/1 "2019-06-13T21:42:16Z")

</div>

I know the solution has a more concise code but I don’t understand why the code I wrote doesn’t work? What am I missing?

Also - I don’t understand why the solution doesn’t return all values in the array that pass the truth test (as it is using a for loop). The challenge asks for just the first one.

solution I refer to is here: ([freeCodeCamp Challenge Guide: Finders Keepers - freeCodeCamp Community - The freeCodeCamp Forum](https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers/))

**Your code so far**

```js

function findElement(arr, func) {

let trueArr = [];

for(let i = 0; i < arr.length; i++){
  if(func(arr[i] = true)){
    trueArr.push(arr[i]);
    return trueArr[0];
  } else {return undefined;}
}

}

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

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36`.

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![colfah](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@colfah](https://forum.freecodecamp.org/u/colfah)\
**Post date:** [June 14, 2019, 9:51am UTC](https://forum.freecodecamp.org/t/finders-keepers-why-doesnt-my-code-work/283875/3 "2019-06-14T09:51:03Z")

</div>

Thanks for that.

I’ve amended the code to correct the function and change location of return function.

I don’t understand why it’s still not working. What am I missing?

```auto
function findElement(arr, func) {
let trueArr = [];

for(let i = 0; i < arr.length; i++){
  if(func(arr[i]) == 1){
    trueArr.push(arr[i]);
    
  } 
  
  else {return undefined;}
}

return trueArr[0];
}

```

---

<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:** [June 14, 2019, 10:01am UTC](https://forum.freecodecamp.org/t/finders-keepers-why-doesnt-my-code-work/283875/4 "2019-06-14T10:01:33Z")

</div>

> [@colfah](#):
>
> if(func(arr[i]) == 1){ trueArr.push(arr[i]); } else {return undefined;}

remember that when you have an else statement at the end, one of the statements in the chain will always execute - you have a return statement inside the loop and a return statement will break from the function returning a value, you have a `return undefined` that will run as soon as the if condition is false

remember that `func(arr[i])` returns a boolean, so you may want to compare it with `true` or `false` not with a number

---

<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 25, 2021, 12:37pm UTC](https://forum.freecodecamp.org/t/finders-keepers-why-doesnt-my-code-work/283875/5 "2021-01-25T12:37:32Z")

</div>

Closed on January 25, 2021, 12:37pm UTC


