Basic Algorithm Scripting - Finders Keepers

Tell us what’s happening:
I am totally lost, on what to do. I know it is asking us to check if the passed arr has an element that can pass the test that we pass as a func but I am lost for what to do. Seem like all the logic is been given when executing the function findElement([1, 2, 3, 4], num => num % 2 === 0); , so I am totally lost on what to do?

Any pointer will be helpful without solution

Your code so far

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

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

Your browser information:

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

Challenge: Basic Algorithm Scripting - Finders Keepers

Link to the challenge:

Following is my latest code

function findElement(arr, func) {
  let num =>{
    if(func(a) === true){
      return num;

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

Not sure what I m doing

I’m not sure on what instance of the course you are, so I’ll explain it using a for loop.
Basically, as you said, the idea is to see what’s the first item in the array that passes func. To do so, what you need to do is to apply func to every item in the array until one passes func. The logic would be:


If you would like to practice, see if you can rewrite this logic using array prototype functions like map, filter or reduce.

I did with .map, am I doing it right?

To me, function like map + ES6 syntax were just too much to try to use them at first. Loops and ifs were my thing. I’ll try to help you understand how you are using that function.

First of all, you need to understand what kind of value should the function findElement return, which is simply a number, while map and filter usually return arrays.

Here you would be assigning the returned value from to a variable named num. Until is run it has nothing inside.

What you are saying here, is that you are going to iterate through this array using map. Each element of the array will be called ‘a’. To each element, I’m gonna check if func(a) calling func with a as argument returns true. If it returns true I’m returning num ¿? . Returning a sounds more appropriate. However, this will apply to every element of the array, eventually returning an array with the elements that passed the function.

At last, when the map is done and num holds the returned array, you function display in console that array, but it never returns a value.

Hope I could explain myself :sweat_smile:

1 Like

Thanks for a great explanation, be it map, filter etc. it is returning the numbers, not the boolean value. why?

so I fixed and it is working now

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

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

one question, what is num on the second line not getting used?

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.


Map is not appropriate here. Map is for creating a brand new array of the exact same length of an old array.

You could use filter - filter is for creating a new array out of elements from an old array that meet a condition - but it would be inefficient.

Reduce could work, though it would be a far more complex toal than you really want for this task - you aren’t synthesizing information across multiple elements to create a result.

1 Like

Why did you create the variable num? What do you want it to do?

You could remove it and your code would still work! Give it a try.

Oh my bad, I’ll definitely have it in mind for next posts.

I wanted to assign num before the for loop what is undefined will be value of return.

Another confusion here for me was what if I just return arr[i] what and who holds its value e.g. what is assignment

You don’t need to make an assignment inside of the function to be able to use return

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