freeCodeCamp Challenge Guide: Finders Keepers

freeCodeCamp Challenge Guide: Finders Keepers
0

Finders Keepers


Problem Explanation

We need to return the element from an array that passes a function. Both the function and the array are passed into our function findElement(arr, func).


Hints

Hint 1

We need to return the element from an array that passes a function. Both the function and the array are passed into our function findElement(arr, func). Looking through the array can be done with a for loop.

Hint 2

num is passed to the function. We will need to set it to the elements we want to check with the function.

Hint 3

Do not forget, if none of the numbers in the array pass the test, it should return undefined.


Solutions

Solution 1 (Click to Show/Hide)
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;
}

Code Explanation

  • Challenge asks us to look through array. This is done using a for loop.
  • The num variable is being passed into the function, so we set it to each index in our array.
  • The pre-defined function already checks each number for us, so if it is “true”, we return that num.
  • If none of the numbers in the array pass the function’s test, we return undefined.
Solution 2 (Click to Show/Hide)
function findElement(arr, func) {
  return arr.find(func);
}

Relevant Links

Solution 3 (Click to Show/Hide)
function findElement(arr, func) {
  return arr[arr.map(func).indexOf(true)];
}

Code Explanation

  1. Look through the array given in the 1st paramater “arr” using the .map() method
  2. Use the function in the 2nd parameter as the callback function in arr.map()
  3. Acquire the index of the first number that meets the condition in the function.
  4. Use that index to display the first available number that meets the condition.
6 Likes
function findElement(arr, func) {
  return arr.find(func);
}

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

You can just use a find array helper method instead of the filter; makes your code even smaller

32 Likes

This could be simple one liner solution which is more modular and obviously less code. Here is how:


function findElement( arr , func ) {
     return arr.filter(func)[0];
}

And that’s about it. :slight_smile:

16 Likes

Or alternatively:

function findElement(arr, func) {
  return arr.filter(func).shift();
}
3 Likes

function findElement(arr, func) {

for (var i=0;i<arr.length;i++){ // loop through the provided array.
if (func(arr[i])) { // if the provided function is called & passed the parameter of the loops current element & returns true,
return arr[i]; // return the current loop element to the console.
}
}
return undefined; // if no element of the provided array passes the functions test return undefined.
}

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

function findElement(arr, func) {
  return arr.find(func);
}
3 Likes

Hi all,

Here is my code:

function find`Eleme`nt(arr, func) {
          //decalre an array to store all value that pass the test
          var arr2 = [];
      
      // add all values that pass the test on func
      arr2 = ( arr.filter(function(el){
        return func(el) ? arr2.push(el) : "";
      }) );
      
      //return first element that passed the test, if it didn't pass the test 
      //it will return undefined - exactly what we need!
      return arr2[0];
    } 
    //test
    findElement([1, 3, 5, 9], function(num){ return num % 2 === 0; });

My solution as well!

One more way. A must try for the one, who want to keep it simple:

function findElement(arr, func) {
  for(var i = 0; i < arr.length; i++) {
    if(func(arr[i])) {
      return arr[i];
    }
  }
  return undefined;
}

1 Like

Looking at other solutions, I could have just chained the shift method to the filter.

function findElement(arr, func) {
  
  let test   = arr.filter(func),
      output = test.shift();
  
  return output
}

findElement([1, 3, 5, 8, 9, 10], function(num){ return num % 2 === 0; });

Did it in the intermediate way in just one line!!!

function find(arr, func) {
  return arr.filter(func)[0];
}
2 Likes

This is my solution, for this problem, it was easy to find the solution for this anyways is a challenge:

function findElement(arr, func) {
  var num = 0;
  for(j = 0; j < arr.length; j++){
    if(func(arr[j]) === true){
      return arr[j];
    }
  }
}

function findElement(arr, func) {

var num = arr.filter(function(a){
return func(a);
});
return num[0];
}

findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; });

function findElement(arr, func) {
  return arr.filter(function(item){
    return func(item);
  })[0];
}

Hello,

Just keeping it simple:

    function findElement(arr, func) {
      
      for (var i = 0; i < arr.length; i++)
        if(func(arr[i])) return arr[i];
    }
    //You make this look easy!
    findElement([1, 3, 5, 8, 9, 10], function(num){ return num % 2 === 0; });