Not understanding the .map() method exercise


**Apologies in advance if I’m not using the right tag, I’m not sure if this belongs in ‘learn-iterators’ or ‘learn-arrays’. But since the exercise I’m confused about is in the learn iterators section I’ll post it here.

So here the objective is to use the .map() to print the first letter of each element in the array and obtain a secret message:

const animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];

const secretMessage = => animal[0]); //Prints 'HelloWorld'

What I don’t undersetand is why it works, since in the line where the secretMessage const is defined we type (animal => animal[0]) , right? but shouldn’t the animal[0] print the first element in the array instead of the first letter of each one of the elements in the animal array?

‘animal’ (singular) is the variable that’s iterating through ‘animals’ (plural - the array).
so, map is going through the array, using each element in turn in the arrow function to take out the first letter and add it to secretMessage.
It needn’t be ‘animal’, could just as easily be any other variable name. It just makes sense contextually.

const secretMessage = => element[0])

would work just the same.

It would be similar to;

for (animal in animals)
  secretMessage += animal[0]

Hope that helps

1 Like