Finders Keepers

Tell us what’s happening:
Hello,
I’m solving this algorithm and
I would like to ask how I can to pass the first argument ‘arr’ to the second ‘func’?

Your code so far


function findElement(arr, func) {
  let num = 0;
  for(let i = 0; i < arr.length; i++){
    
  }

  return num;
}

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/64.0.3282.119 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

func is a function given as an argument to findElement. You can use it as such within your findElement function by calling:

func( x )

Where “x” is your parameter. In this case func expects a number so the following function:

function findElement(arr, func) {
    return func(5);
  }

would return false.

passing your arr in func won’t work as arr is an array, so you will have to iterate this to come to the final result.

1 Like

func does not take an array parameter. func accepts a single number parameter and returns true or false depending in whether that number passes some test.

You need to test each number in array until you find the first one that passes the test in func. Return that number

1 Like