Build a First Element Finder - Build a First Element Finder

Tell us what’s happening:

i know i’m wrong answer not depending on function as parameter but i donno how , i passed all tests except one if i right condition for it it don’t pass to , could anyone help

Your code so far

function findElement(arr, func){
  let stash = []

  for (let i = 0; i < arr.length; i++){
    if(arr[i].length > 5) {
       stash.push(arr[i])
    }
    if(arr[i] % 2 == 0) {
       stash.push(arr[i])
    }
  }
  return stash[0] 
};
console.log(findElement([1, 3, 5, 8, 9, 10]))

Your browser information:

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

Challenge Information:

Build a First Element Finder - Build a First Element Finder

you need to use the func parameter

please attempt to do that

can u throw me to the lesson that help me to do that please , sorry !

closures i think ? isn’t it ?!

Read the instructions of the project again.

function findElement(arr, func){

You’ve created a function with 2 parameters. arr and func

In the example code, you are to call the function with 2 arguments:

findElement([1, 3, 5, 8], num => num % 2 === 0) // returns 8
findElement([1, 3, 5], num => num % 2 === 0)    // returns undefined

You are only using one argument:

findElement([1, 3, 5, 8, 9, 10])

And your code never refers to the func parameter

i see , i try brain storming , but iam a big failure !

i will try to eexplain to you what i met is hard ! the func that i use as parameter how can i pass a parameter to it ? i donno how , could you gimme hint !

You’ve put in the example test here:

But instead of hard coding this test arr[i] % 2 == 0 you need to pass it as an argument when you call the function.

Please ask if you have any questions

Just as you see in the example function calls

findElement([1, 3, 5, 8], num => num % 2 === 0) // returns 8
findElement([1, 3, 5], num => num % 2 === 0)    // returns undefined

num => num % 2 === 0 will be passed into the func parameter

so i don’t need to pass the condition on the function instead i pass it in the console when i call the function this is what ya mean right ?

Basically, yes. You pass it just as you see in the example function calls.

It should not be written hardcoded into the function. This way the function can only do one kind of test.

where can i get num as a parameter sorry ?!

You don’t really need to worry about num. Within your function that function will be known as func and you can pass numbers to it.

1 Like

my brain not working still , i think i need to delay it maybe later i work nicely with it , maybe i will try another challenge or try to study somehow

This might help:

https://www.geeksforgeeks.org/javascript/passing-a-function-as-a-parameter-in-javascript/

1 Like

remember when you create a function like function myFunc (...) {...} you then call it myFunc(...), use func like a normal function, which it is

i will try :frowning: i think i will get a big smile when i get finished of it

if you talk about camelCase naming i know about it , cuz iam old learner but i still donno where to put conditions on the sub func and when i call it on the console i get an error

function findElement(array, func) {
  for(let i = 0; i < array.length; i++) {
  if(func(array[i] == true) )
  return array[i]
  }
  
}

console.log(findElement([1, 3, 5, 8, 9, 10], func(x) => x % 2 == 0))

now you are using func, but double check what you are giving it as argument, is it only the array element?