# Basic Algorithm Scripting - Finders Keepers

Tell us what’s happening:
Describe your issue in detail here.
can anyone explain what happen in this code to me, there is no code explanation in the guide so i do not understand this code. Thank you very much

``````function findElement(arr, func) {
return arr.length && !func(arr[0])
? findElement(arr.slice(1), func)
: arr[0];
}

console.log(findElement([7, 2, 3, 7], num => num % 2 === 0));
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36`

Challenge: Basic Algorithm Scripting - Finders Keepers

I have some questions, sir :
1.First, does !func(arr[0]) means that if the condition at arr[0] is not true( 1 is not divisible by 2), then it will do sth, right?

2.Why do we have to return the length of the arr?

3.The

``````exprIfFalse
``````

just shows arr[0]
then how does the computer know to return undefined? Thank you.

Not quite what you mean with your first question, but its not returning the length of arr, its basically checking if arr is empty by checking for falsy values. `0` is falsy but any other number is truthy so if there is anything in arr it will be true. If arr is not empty it then goes to the other side of `&&` and uses the provided function to check if the first number in arr is even or odd. But `!` is inverting the true/false return of that function, so if its odd it turns it to true, if it is even it turns it false.

Because of the `&&` both need to be true for it to recursively call the `findElement` function, else it returns `arr[0]`. And its calling the recursive function minus the first element in arr, so each time its just checking the first element in arr till it finds an even number, or till arr is empty.

The first condition is if the `arr.length` is `0` (empty) it will be false and return `arr[0]`. If arr is empty `arr[0]` will be nothing, and by default that will be `undefined`, which is where you get your `undefined`.

If `arr.length` is true it then checks with the provided function if arr[0] is odd/even. The inverted true/false says its false if its even, and also returns `arr[0]` which in this case would be the first even number.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.