Hey, just wanted to ask: Shouldn't the callback of .filter() return a truthy/falsey value? I guess it works in this case, but it could be confusing if someone tried to use filter in the sense of how .map() is supposed to be used. Also this probably fails if yummies contained a falsey value like an empty string which it wanted to keep.
Hi,the students for whom this was written only know some basic array methods and haven't done map() as yet. So I wrote this article around what they already know, as beginners getting into advanced JS. The filter() method iterates over each element in the array and passes it to the filterYums function. Yes, if the condition yummies.indexOf(currentItem) === index is met it will return true and that element will be passed to the new array. The condition does not check for falsey values like empty strings, null and undefined.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hey, just wanted to ask: Shouldn't the callback of .filter() return a truthy/falsey value? I guess it works in this case, but it could be confusing if someone tried to use filter in the sense of how .map() is supposed to be used. Also this probably fails if yummies contained a falsey value like an empty string which it wanted to keep.
Hi,the students for whom this was written only know some basic array methods and haven't done map() as yet. So I wrote this article around what they already know, as beginners getting into advanced JS. The filter() method iterates over each element in the array and passes it to the filterYums function. Yes, if the condition yummies.indexOf(currentItem) === index is met it will return true and that element will be passed to the new array. The condition does not check for falsey values like empty strings, null and undefined.