# Can some one explain me how this code is working this is from basic algo scripting finders keepers chellenge

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

Well, lets compare it to your solution. What code did you write for this challenge?

actually I was going all the solutions I got other solutions but did not get it and I also know recursive function that has been used in this logic

Its much, much easier to explain this solution if you have solve this problem already. If you haven’t solved this problem, how far did you get? Where did you get stuck? What code have you written?

``````function findElement(arr, func) {
let num = 0;

for (let i = 0; i < arr.length; i++) {
num = arr[i];
if (func(num)) {
return num;
}
}

return undefined;
}
``````

I wrote this solution to solve the task

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

oh thank you I will take care of that next time

So lets start comparing:

``````function findElement(arr, func) {
let num = 0;

for (let i = 0; i < arr.length; i++) {
num = arr[i];
if (func(num)) {
return num;
}
}

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

Initially these look pretty different. Lets expand the second one a bit:

``````function findElement(arr, func) {
if (arr.length && !func(arr)) {
return findElement(arr.slice(1), func);
} else {
return arr;
}
}
``````

Ok, this is starting to look a little bit similar… You have a loop but they have recursion. Let’s dig into the condition for the `if`

`if (arr.length && !func(arr)`, well, this says “if there are elements in the array and the function return value is not truthy for `arr`, then call this function recursively (i.e. keep looking)”.

Otherwise (if there array is empty or the function return value is truthy), then return `arr`. That seems to match up with your

``````    num = arr[i];
if (func(num)) {
return num;
}
``````

Does this help or do we need to unwind deeper?

2 Likes

thanks a lot sir I understood the code I try to explain it so please correct me if I am not

``````findElement([1, 2, 3, 4], num => num % 2 === 0)
``````

if I call the the function with above arguments so on this line

``````arr.length && !func(arr)
``````

it will check whether there are elements in the array or not && func which is taking the
very first element of the given array as an argument , will check whether this number passes the test thus if the number on this index passes the test it returns true and if it does not pass the test it returns false and when it returns false it will run this line of code

`````` findElement(arr.slice(1), func)
``````

it is calling the function recursively now it is giving the first parameter of function

function findElement(arr, func)
a new array after slicing it so in my condition arr = [ 2, 3, 4] after slicing now function will run again and on this line

``````arr.length && !func(arr)

``````

it will check again whether there is something in array && func(arr) returns true or false
in the first round arr = 1 so it will not pass the test but in the second round arr = 2 it will the test that is a function given as an argument and runs the else statement

Bingo. Sounds like you understand it!

thanks a lot sir you guys are giving a lot to the community

I did not get this solution also can you explain it please or someone else

``````
function mutation([ target, test ], i = 0) {
target = target.toLowerCase();
test = test.toLowerCase();
return i >= test.length
? true
: !target.includes(test[i])
? false
: mutation([ target, test ], i + 1);
}
``````

this is from the chellange of mutations from basic algorithm scripting