# Basic Algorithm Scripting: Finders Keepers - What is a *truth test*?

Tell us what’s happening:
Can someone please help clarify the instructions - specifically, What is a truth test?

Also, in the tests to check whether we solved the challenge, it seems they are using different arguments for the first parameter (arr) but the same argument each time for the second parameter (func). Wouldn’t we want to test with different arguments for both parameters?

Your code so far

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

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

``````

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36`.

Challenge: Finders Keepers

The “truth test” is the `func` argument. It’s a function that returns a boolean value. Your function doesn’t need to know what that function is, just that it will take a single parameter and return a boolean value.

Both tests are using a callback that checks (returns `true`) if the number in the array is an even number. The first test is run on an array with even numbers in it and the second is run in an array with no even numbers in it.

How do we know that the argument we put in will always return a boolean value?

Nope, we know that the function will return a boolean.

For example

``````num % 2 === 0
``````

expects returns `true` if `num` is evenly divisible by 2.

Nope to what? Sorry I’m unclear. My question was how do we know (not a yes or no question).

I understand that this particular function num % 2 === 0 will return a boolean but it seems we can put any argument in as a parameter - we could put an array, a string, etc. Also for that matter, we could put any argument for the first parameter as well, right? The function doesn’t seem to stipulate what kind of arguments we put in, right? I’m a bit confused.

Thanks for the link. What is the relationship of truthy to this particular exercise? What is a callback?

Most of the challenges on freeCodeCamp don’t expect you to validate your input variables. It’s often good practice, but in academic exercises you can trust that the function parameter isn’t going to throw an error.

Ok. I think I understand the part about not needing to validate the input for an academic exercise.

But without validation how would we know to use an array for the first argument and a function and especially this particular function for the second argument?

I see that we are fed specific info (a couple of arrays to test using the same function num) but it seems arbitrary. I think I’m having trouble understanding the point of this exercise (meaning I am not sure what I am expected to solve or what to learn from it).

You’re just told that your function must accept an array as the first element and a function as the second.

Although there are potential real-world uses for something like this, the main point of the exercise is to familiarize you with using functions as first class objects that can be passed as an argument.

Ok, thanks for the clarification. I think the instructions could be a bit more clear (I find them to be vague as I often find the FCC instructions).

It relates to the callback function and how it is used to test for some condition. The way it was written is potentially a bit confusing to new learners, I’m guessing it maybe was done to save some space.

To write it out more explicitly and show the idea behind it I kind of have to give the solution to the challenge so I will blur it.

``````const elements = [1, 4, 6, 7, 9, 10];

function isOdd(num) {
return num % 2 !== 0;
}

function isEven(num) {
return num % 2 === 0;
}

function isGreaterThanEight(num) {
return num > 8;
}

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

console.log(findElement(elements, isOdd));
// 1
console.log(findElement(elements, isEven));
// 4
console.log(findElement(elements, isGreaterThanEight));
// 9
``````

Notice how we are able to pass different functions into findElement. It loops the array and calls the callback function passing in the array elements. When the callback returns true the findElement returns that element.

Edit: It is like implementing the find array method.

``````console.log(elements.find(isEven));
// 4
``````

A callback is a function that is called at some later time. For example, all the array methods you know about take a callback function.

``````// num => is the callback
[1,2,3].forEach(num => console.log(num))

// The more explicit version, the callback is the anonymous function
// it gets called by forEach on each loop iteration
[1,2,3].forEach(function(num) {
console.log(num)
})

// Here is a simple version of a forEach implementation
// this is a stand-alone function and not on the array prototype
function forEach(array, callback) {
for(var i = 0; i < array.length; i++) {
callback(array[i], i, array);
}
}
``````

Thanks for the detailed explanation, this is helpful.