freeCodeCamp Algorithm Challenge Guide: Finders Keepers

freeCodeCamp Algorithm Challenge Guide: Finders Keepers
0

#1

:checkered_flag: Problem Explanation:

The problem is quite simple to understand. You will check for each element in the array that is passed in the first argument, if the element plugged in to the function passed as the second argument returns true the first time. We do not care about the second or third one that is true, only the very first one if any. If there are none, then return undefined. This last bit is not explained but it is part of the tests used.

Relevant Links

str.substr()

:speech_balloon: Hint: 1

You can use the function directly from the parameter, no need to rename it or anything.

try to solve the problem now

:speech_balloon: Hint: 2

You need to pass an element and record it if the function returns true, for this you just have to pass the element as the parameter for the function.

try to solve the problem now

:speech_balloon: Hint: 3

If no element satisfy the function then you must return undefined

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:beginner: Basic Code Solution:

function findElement(arr, func) {
  // Make num undefined by default
  var num;

  // Loop thorugh the array and use the function to check
  for (var a = 0; a < arr.length; a++) {
    if (func(arr[a])) {
      // Store the first case and break the loop
      num = arr[a];
      return num;
    }
  }

  // otherwise return undefined
  return num;
}

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

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

:rocket: Run Code

Code Explanation:

  • To make the code easier, create an undefined variable that will be returned.
  • Loop through the array to check for each element if it satisfy the function. This is done by passing the arr[index of the loop] as the parameter for the function from the second argument.
  • If true, then store the array element, and return it. This will stop the loop. No else needed.
  • If the loop was not broken and it has ended, then return num which by default is undefined. This means that none of the elements from the array satisfied the function.

Relevant Links

:sunflower: Intermediate Code Solution:

function findElement(arr, func) {
  filterArr = arr.filter(func); //filter array with the function provided

  return filterArr[0]; //return the first element that returns true, or undefined if no elements return true
}

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

:rocket: Run Code

Relevant Links

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories β€” Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: Wiki Challenge Solution Template for reference.


Basic Algorithm Scripting: Finders Keepers
#2

#3

#4
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


#5

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:


#6

Or alternatively:

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

#7

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


#8
function findElement(arr, func) {
  return arr.find(func);
}

#9

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

#10

My solution as well!


#11

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


#12

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

#13

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

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

#14

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];
    }
  }
}

#15

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


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

#17

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