Finders Keepers while loop solution

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:


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.

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 .

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:

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

is functionally the same as

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

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.

@vaccarieli The following produce the same result;

const someArray = [ 'a', 'b', 'c' ];
for (let i = 0; i < someArray.length; i++) {
  console.log(someArray[i]);
}

OR

const someArray = [ 'a', 'b', 'c' ];
let i = 0;
while (i < someArray.length) {
  console.log(someArray[i]);
  i++;
}

OR

const someArray = [ 'a', 'b', 'c' ];
for (let i = 0; i < someArray.length;) {
  console.log(someArray[i++]);
}

OR

const someArray = [ 'a', 'b', 'c' ];
let i = 0;
while (i < someArray.length) {
  console.log(someArray[i++]);
}

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.

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));