Basic Algorithm Scripting: Finders Keepers

Basic Algorithm Scripting: Finders Keepers
0

#1

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.


#3

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].


#4

What was his answer? I also tried to solve it exactly the same way using map…


#5

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];


#6

What about that:

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

The find() method does exactly the job.


#7

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


#8

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


#9

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);
}
}


#10

That is great that you came up with a solution, but next time instead of posting full working solutions to the FCC challenge, stick to answering the question(s) of a thread’s original poster. You can do that with hints and suggestions, but please do not post “your” full working solution.


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

it works.


#12

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!!!


#13

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)?


#14

The arr.filter(func) returns an array, so the [0] is referencing the first element of the returned array.

The filter does return an array which could be zero, one or more elements. Above, I explain how only the first element is returned.


#15

I apologize. Will take note next time.


#16

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…


#17

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: