Finders Keepers: what to do with num?

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


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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

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

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.

1 Like

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:

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.

1 Like