Basic Algorithm Scripting: Finders Keepers

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/finders-keepers

So, I know how to solve this one with a for loop. And I read this post (freeCodeCamp Algorithm Challenge Guide: Finders Keepers) so now I know how to solve it using .filter().

I tend to reach for .map() a lot when I would have used a for loop before, but I couldn’t get this working using .map().

This is more or less what I was trying:

function findElement(arr, func) {
  return arr.map(num => {
    if(func){
      return num
    }
  });
}

Can anyone explain where I went wrong? Thanks.

That makes sense. The way I was thinking of it was that num would only be returned if func evaluated to true, so it would work the same way that using .filter() would, and you would return newArr[0].

There are ways to use map() in this challenge, but not without writing more code than necessary. This is a job for filter().

Take a look at the following minimalist solution. If it makes little sense, try to rewrite it more verbosely. It uses arrow syntax, which allows you to omit curly brackets and the words function and return .

let findElement = (arr, func) => arr.filter(func)[0];

2 Likes

What about that:

function findElement(arr, func) {
  let num = 0;
  return arr.find((num) => { return func(num)});
}

The find() method does exactly the job.

3 Likes

Yes, best solution except no need:
let num = 0;

Sure, I forgot to delete it as it was in the template provided.
Thanks for the hint!

Hey guys. I got a solution that is working, just wanted to hear your feedback.

function findElement(arr, func) {
if (func(arr[0]) === true) {
return arr[0];
} else if (arr.length === 1) {
return undefined;
} else {
return findElement(arr.splice(1, arr.length-1), func);
}
}

let num  =  arr.filter(func);
return num[0];

it works.

1 Like

Thank you all for your responses. I will try and honor the moderator’s request to not simply post a solution.
I found the simplest code solution to be with the find() method. You can literally write one line inside the function.
I hope this helps.
https://www.w3schools.com/jsref/jsref_find.asp

HAPPY CODING!!!

Thanks for the solution. I understand most of it but I am unsure of:

  1. What the “[0]” do at the end of that function?

  2. How does the function know only to return the first item and not multiple? (as filter would return multiple)?

I apologize. Will take note next time.

First, I solved this challenge without using the func (function)… then I went back and after seeing your post I considered I could try to solve this using the Map() function as you suggested… which I solved ,
however, it still involved a For loop due to the fact that the Map() function creates an array instead of a string Sooooo… it looked more like

let myArr=arr.map(func);
  for(let x=0;x<myArr.length;x++)

in order to sort through the array to find the truth value… It is however in the end cleaner apparently to use the filter method suggested after your post. But I thought I would leave this here just in case you still wanted to use map() instead…

Hello,

I solved it by iterating through the array with a for and then returning the first item which fulfilled the func condition. Pretty straight forward. :slight_smile:

Hi there…

Without using filter
I want to solve the problem using map, but the returned value is a boolean array.
How can I get that value if true?

2 posts were split to a new topic: Help woth FindersKeeper

function findElement(arr, func) {

//let num = 0;

for(let i=0;i<=arr.length;i++)

{

if(arr[i] % 2===0){

  return arr[i];

}

}

}