Stuck on Suggested Reading How To Use Array Methods in JavaScript: Iteration Methods

I get this stuff, mostly, but I had trouble with this example…

let seaCreatures = ["whale", "octopus", "shark", "cuttlefish", "flounder"]; const isCephalopod = cephalopod => { return ['cuttlefish', 'octopus'].includes(cephalopod); }; seaCreatures.find(isCephalopod); //expected output "octopus"

MDN documentation for .includes() says it returns boolean. Can someone please explain how we make the jump to returning octopus with this? They use it as an example, though the documentation doesn’t explain this kind of functionality.

Then the article continues to use the same example to explain .findIndex() but makes as little sense to me.

I won’t be able to rest and move foreward until this makes sense to me.

OK, so this is more about find() than it is about includes(), which is why the example is a little confusing.

seaCreatures.find(isCephalopod) is just returning the first item in the list for which your callback function is true.

Your callback function will return true for either cuttlefish or octopus, thanks to includes()

The first item out of these is octopus.

Thank you for your answer, you are right!! When reading it, and prior to practicing iterators, the idea was hard for me to grasp. But here’s my way of stating the same thing (for the sake of solidifying it for myself and others)

I was hung up on the idea that:
[‘cuddlefish’, ‘octpus’].includes(cephalopod) must return true,
per the documentation.

And couldn’t connect the dots to the final “octopus” output. But that line DOES return true, but only within the scope of the method .find(). That find method takes the true statement and returns what element in the array it was on when it received its first true, which happened to be ‘octopus’ .

Thanks for the support, hopefully this helps someone else also.

1 Like