Build a First Element Finder - Build a First Element Finder

Tell us what’s happening:

JAVA SCRIPT
I am not sure if it works properly, I passed all the test and like the console gives what it asks, but i think i am lost somewhere because i completed it so fast? tell me please

Your code so far

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

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

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

1 Like

Not sure I understand what you’re asking. Do you have a more specific question?

I just want to make sure is my code fully correct? I am just lost because i completed it way too fast on my opinion, like it has two the same 'if’s

I see what you mean.

What is the purpose of the second if ?

How long should it take?

you have an if and an else if with the same condition, do you think the else if is ever going to be executed?

yes? i can see it executed, or am i just lost and the answer is just “undefined” and no matter do i have second if or not? Thats actually why am i asking for, i cant understand why does my code works perfectly, with that type of strange things

how do you see it executed?

because when you have an if followed by an else if, the first one with the true condition will execute, and they are both true or both false, so the else if never executes

1 Like

i dont know i have some ‘undefined’ console commands, on the places where they should be, i can get how

check this

function myfunc() {}

console.log(myfunc())

what do you think will print to the console?

ah, okaaay, it makes sense that the return in the function is undefined, so it gives back ‘undefined’

but i still cannot get how to put a return for the function, if my function argument is not static, im just lost, already surfing the web for maybe any help, nothing..

what do you mean? your function returns

it returns ‘undefined’ , am i right?

sometimes it does, but it does not come from the return undefined line

i understand that, i cant get what shold i change so it will

you have written that it should return undefined when func(x) is true, is that what should happen?

nooo, should it return undefined when func(x) is false

just when it is false? so if the first item in array makes func(x) be false the function should return undefined and stop there?

no, as i understood it should check all the items in array, and i case every items makes func(x) to be false, it should return undefined. Am i right?

and is it doing that correctly now, right?

do you understand why it is doing that?