Build a First Element Finder - Build a First Element Finder

Tell us what’s happening:

Thx guys, Ive seen a bunch of giant codes on forums. but for its work. I would like to know if a short function like that is safe. THX omw studying code

Your code so far

function findElement (arr,funct){
  
  for(let search of arr){
     if(funct(search)){
      return search;
    }
  }
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36

Challenge Information:

Build a First Element Finder - Build a First Element Finder
https://www.freecodecamp.org/learn/full-stack-developer/lab-first-element-finder/build-a-first-element-finder

Not all functions need to be super big lines of code. Sometimes something small like that works just fine.

Length does not equal safety.

We have blurred this solution (with [spoiler][/spoiler] tags) so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

1 Like

May I ask a question? It would be possible to solve this test with the basic for loop? The “basic” loop i mean for(let i=0; i<arr.length; i++)… Thank you for any reply.

Hi @Ariyia

Just about anything is possible with code, and there are may approaches and methods you can use. Give it a go.

Happy coding

1 Like