function findElement(arr, func) {
arr.filter(func) ? arr.filter(func)[0] : undefined
}
findElement([1, 2, 3, 4], num => num % 2 === 0);
// It passes the first one, but on second call,
// test doesn't pass, saying "Should return 8".
findElement([1, 3, 5, 8, 9, 10], num => num % 2 === 0)
What am I missing ? Because if I log, the answer is good
@GregFR You solved the problem and that’s great! I’m not trying to get you down, but there’s always room for improvement so I want to point some things out.
Filter creates a new array in memory with all the values that return a truthy value when passed into func. In a worst-case scenario, let’s say you have an array made up of a million elements, and every element is the string “foo”. Let’s say that the func callback is x => x === 'foo'. When you pass those to filter, it’s going to make a new million-item-long array of “foo” in memory. This creates a lot of waste, especially when the element has already been found at index 0, there was no need to check the rest of the 999,999 elements.
Secondly, semantically when I see filter in a piece of code, I expect that the filtered array has a purpose as to be an array. In this case it does not, all we care about is a single element.
So the reason I suggested the find method above was not only because it’s simpler to read and understand, it’s also more efficient (it short-circuits once it has found an element). If not the find method, then a for loop could also be written to short-circuit.
I understand if you’ve just learned about the filter method and want to get used to using it, but I just don’t think it’s the right tool for the job in this situation.
I totally agree with you. I was aware already that filter goes through the entire array, thanks for a project I made (I was checking if filter.length was true, but I replaced with arr.some, awesome method also), and here I got mislead indeed with the findElement having just an tiny array so I went with filter
I understand that find does the job perfectly here, thank you for all your information. Very interesting to read that again (until it fixes itself in my head ^^)