# Finders Keepers while loop solution

**URL:** <https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231>\
**Category:** General\
**Created:** [August 22, 2019, 9:15pm UTC](https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231 "2019-08-22T21:15:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaccarieli](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vaccarieli/32/166989_2.png) [@vaccarieli](https://forum.freecodecamp.org/u/vaccarieli)\
**Post date:** [August 22, 2019, 9:15pm UTC](https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231/1 "2019-08-22T21:15:09Z")

</div>

Hi guys , I am currently at the Basic Algorithm Scripting: Finders Keepers. which tells me to 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.

My question is how to get this or why is not working the while loop the same way the loop does. any suggestion would be greatly appreciated.

I did this:

```auto

function findElement(arr, func) {
  let num = 0;
  
  for(var i = 0; i < arr.length; i++) {
    num = arr[i];
    if (func(num)) {
      return num;
    }
  }
  
  return undefined;
}
console.log(findElement([8, 4, 3, 4], num => num % 2 === 0)); 

// here the output is = 8, which is fine since the condition was met in the first number of the array which causes the loop to end and return the desired value.

```

So, everything works good with the for loop condition, my real problem or I guess question is how to get the same result using the while loop.

```auto
function findElement(arr, func) {
  let num = 0;
  while (num < arr.length ){
    num++;
    num = arr[num];
    if(func(num)){
      return num;
    }
  }
  return undefined;
}
console.log(findElement([8, 4, 3, 4], num => num % 2 === 0));  
// this is the output = 4 .

```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [August 22, 2019, 9:42pm UTC](https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231/2 "2019-08-22T21:42:46Z")

</div>

You are first treating `num` like an iterator, in your `while` condition, then you are assigning it to an array value which you test against the function.

For future reference, the following is sort of abuse of a `while` loop. You’re just reinventing a `for` loop in a more verbose and less readable way:

```javascript
  let num = 0;
  while (num < arr.length ){
   ...
    num++;
}

```

is functionally the same as

```javascript
for(let num = 0; num < arr.length; num++) {
    ...
}

```

---

<div class="post-metadata">

**Author:** ![vaccarieli](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vaccarieli/32/166989_2.png) [@vaccarieli](https://forum.freecodecamp.org/u/vaccarieli)\
**Post date:** [August 22, 2019, 10:19pm UTC](https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231/3 "2019-08-22T22:19:01Z")

</div>

thanks for your response, however I am still not clear enough , I would like just to know if it is possible to use the while loop in this example, and yes i know I reinvented the way of how while loop works, cuz I just want to know all the possibilities for each example, I barely have a month studying javascript, don`t know any other computer language, but i would like to get as much as possible information on how to do everything in the rigth way, and pratice as much as i can, thanks for your support.

---

<div class="post-metadata">

**Author:** ![vaccarieli](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vaccarieli/32/166989_2.png) [@vaccarieli](https://forum.freecodecamp.org/u/vaccarieli)\
**Post date:** [August 22, 2019, 11:30pm UTC](https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231/5 "2019-08-22T23:30:49Z")

</div>

Thank you, I managed to figure out the solution, with your answer and ArielLeslie help aswell, thanks. Here is what I did for future reference, if anybody somehow needs it.

My mistake was as ArielLeslie mentioned it before I was altering the function using the iterator against(as parameter) it.

```auto
function findElement(arr, func) {
  let i = 0;
  while (i < arr.length) {
    let num = arr[i];
    i++;
    if (func(num)) {
      return num;
    }
  }
  return undefined;
}
console.log(findElement([5, 5, 3, 7, 9, 7, 7, 3, 10, 8], num => num % 2 === 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:** [January 16, 2021, 6:42pm UTC](https://forum.freecodecamp.org/t/finders-keepers-while-loop-solution/308231/6 "2021-01-16T18:42:52Z")

</div>

Closed on January 16, 2021, 6:42pm UTC


