# Finders Keepers: what to do with num?

**URL:** <https://forum.freecodecamp.org/t/finders-keepers-what-to-do-with-num/312828>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 7, 2019, 10:27pm UTC](https://forum.freecodecamp.org/t/finders-keepers-what-to-do-with-num/312828 "2019-09-07T22:27:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leoncpaulo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/leoncpaulo/32/143934_2.png) [@leoncpaulo](https://forum.freecodecamp.org/u/leoncpaulo)\
**Post date:** [September 7, 2019, 10:27pm UTC](https://forum.freecodecamp.org/t/finders-keepers-what-to-do-with-num/312828/1 "2019-09-07T22:27:15Z")

</div>

**Tell us what’s happening:**  
I don’t know how to pass the arr through the function, nor how to update num with the right arr element that fits into the function in order to get === 0

**Your code so far**

```js

function findElement(arr, func) {
  let num = 0;
  for (let i = 0; i > arr.length; i++) {
    num // what to do with
    arr[i].indexOf(func) >= 0 ? num : undefined;
    
  }
  return num; //it supposed to be the matched arr[i]?
}

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

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119`.

**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:** ![JanShah](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/janshah/32/80384_2.png) [@JanShah](https://forum.freecodecamp.org/u/JanShah)\
**Post date:** [September 7, 2019, 10:45pm UTC](https://forum.freecodecamp.org/t/finders-keepers-what-to-do-with-num/312828/2 "2019-09-07T22:45:59Z")

</div>

the function passed into findElement can be called as any other function would, with the required argument

```auto
var someAnswer = func(argument);

```

In this case, you’re passing in each number to get a true or false answer. When the first true answer is given, return the number associated with it. The instructions specify undefined if a match isn’t found so you only need to concern yourself with a value that returns true when it’s passed into the function ‘func’.

You could console log everything to see what’s going on.

---

<div class="post-metadata">

**Author:** ![MrBanh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mrbanh/32/123419_2.png) [@MrBanh](https://forum.freecodecamp.org/u/MrBanh)\
**Post date:** [September 7, 2019, 11:37pm UTC](https://forum.freecodecamp.org/t/finders-keepers-what-to-do-with-num/312828/3 "2019-09-07T23:37:44Z")

</div>

I think you should first look at your `for` loop, it looks like an infinite loop. As you are looping through the `arr` array, you can access each element of the array using the index with the first element of the array starting at index 0.

For example, using the `arr = [1, 3, 5, 8, 9, 10]`, `arr[0]` returns 1, `arr[1]` returns 3, `arr[2]` returns 5, `arr[3]` returns 8, etc…

So now, you need to know how to use the `func`.

Example: `findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0);`

- `arr` will be `[1, 3, 5, 8, 9, 10]`
- `func` will be `num => num % 2 === 0`. This function takes in a `num` and will return a boolean value. It will return `true` if it’s divisble by 2 (since an even # divided by 2 gives 0 remainders), `false` otherwise.

So what you want to do is call `func` on each element of `arr`. This is where your `for` loop comes in.

- At index 0, `arr[0]` is 1. `func(arr[0])` evaluates if `1 % 2 === 0`, which returns false. Since that returns false, go to the next index.
- At index 1, `arr[1]` is 3. `func(3)` evaluates to false as well.
- At index 2, `arr[2]` is 5. `func(5)` evaluates to false as well.
- At index 3, `arr[3]` is 8. `func(8)` evaluates to true. Since it is true, you can set `num` = 8 and `break` the for loop.

You can return `num` as the answer. However, if you have an `arr = [1, 3, 5, 9]`, nothing satisfies the `num % 2 === 0` condition since they’re all odd numbers. Personally, I would have set `num` to undefined like so:

```auto
function findElement(arr, func) {    
    let num = undefined;
    for (...) {
        ...
    }
    return num;
}

```

So if the for loop couldn’t find anything that satisfies the `func` call (that returns true), `num` will still stay as `undefined` and it will return `undefined`. It there is an element in the array that has `func` return `true`, `num` will be set to that element’s value.

---

<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:16pm UTC](https://forum.freecodecamp.org/t/finders-keepers-what-to-do-with-num/312828/4 "2021-01-25T12:16:55Z")

</div>

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


