Trouble on Finders keepers

Tell us what’s happening:

Create a function that looks through an array arr and returns the first element in it that passes a 'truth test'. 
This means that given an element x, the 'truth test' is passed if func(x) is true. 
If no element passes the test, return undefined.
1. create num function and leave it at 0
2. create for loop to loop through array
3.inside for loop create if statement to compare func to the array
  a. Integrate Num into if statement 
4. place that value in num and return num
function findElement(arr, func) {
  for (let i = 0; i < arr.length; i++) {  
    let num = arr[i];
    if (func(num[i]) == true) {
     return true 
    } else {
    return undefined

console.log(findElement([1, 2, 3, 4], num => num % 2 === 0));

Hey everyone! I’m having trouble with how num is supposed to fit in because it provides the first test case as undefined which is fine since that what it’s supposed to return but I can’t get the first to come as a true

I’m open to suggestions

P.S I will be responding tomorrow morning as I’m offline after I post this.

Thanks :grinning_face_with_smiling_eyes:

What does “create num function and leave it at 0” mean?

    let num = arr[i];
    if (func(num[i]) == true) {

What? What? arr is an array of numbers, therefore num is a number. So, what is num[i]? I would expect that to always be undefined. To be honest, I don’t understand why you need the num variable.

OK, so you have that issue with the num.

Also, when you find the match, what are you supposed to return?

And the return undefined - it’s the right idea but in the wrong place. Because you have an if/else and each of them return, you will never get past the first element. You need to move return undefined somewhere where it will only return after everything has been checked.

If I fix those three things, your code passes for me.


this explains why only one test case passed.

So should I instead place return undefined outside an if/else chain?


I also am confused with num because the test case says

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

I feel that if the test case has num inside I must implement it somehow.


OK I have debugged and tested and it works what’s you feedback on this answer:

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

I finally found that num doesn’t actually mean anything so instead I did what you said and got rid of else and instead num undefined before the if statement because that way num would only return something if it was true and return undefined if it did not pass.

Thanks for your help!

Yeah, that’s basically what I have. I actually moved the return undefined outside of the loop. You removed it which works too because not returning anything is the same as returning undefined.

Cool, good work.


Thanks! I’m still a bit confused over why the test case uses num if num isn’t an input in the function, but I suppose that question is best left alone. I also am not sure how returning arr[i] brings 8. Does it bring the value that the loop was at when the if statement came true? That’s my theory but thanks for the feedback and help!


It’s just the arbitrary parameter name used in the callback function. It has nothing to do with the functioning of findElement - which doesn’t know what variables are called inside the callback function and doesn’t care.


function findElement(arr, func) {
  // ...

findElement([1, 2, 3, 4], num => num % 2 === 0);
// findElement([1, 2, 3, 4], function(num) { return num % 2 === 0; });

could be

function findElement(arr, func) {
  // ...

const myData = [1, 2, 3, 4];
const myCallback = num => num % 2 === 0;

findElement(myData, myCallback);

The findElement function doesn’t care one iota what is going on inside that callback. You could also do:

const myCallback = elephantPajamas => elephantPajamas % 2 === 0;

and it would have no effect on how findElement works and findElement would have no way of knowing.

Does that make sense?

HI @Cy499_Studios !

In the first test case, the computer is looping through this array

[1, 3, 5, 8, 9, 10]

arr[i] represents that current number in the array.
The first number in that array that passes the truth test gets returned.

This is the truth test we are working with in this test case.

num % 2 === 0;

The first three numbers are odd (1,3,5) and fail the test. None of those numbers would give us a remainder of 0

1 % 2 === 0 // false
3 % 2 === 0 //false
5 % 2=== 0 //false

The fourth number in that array (8) would pass the test.

8 % 2 === 0 // true

8 divided by 2 gives us 4 with a remainder of 0.
So 8 is the first number to be returned from that array.

Also, if you are interested in another solution, there is a neat array method that could work too.

Hope that helps!

Thanks @jwilkins.oboe !

That was similar to my thought process that arr[i] stopped the loop at the value that returns true and put that value into num to return.


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