FAQ: Iterators - The .map() Method

I tried using this method and wrote my code like this:

const secretMessage = animals.map(animal => {

return animals.charAt[0]

Which I assumed would call the first letter from animals but it gives me an error.

I’m sure it something very simple but I just can’t seem to work this one out.

Remember to act upon the element in the iteration, not the array object, itself.

animal.charAt(0)

Given such a simple return value we can use implicit return and drop the curly braces. See my example.

Note also that charAt() is a method, not a subscriptable object.

Thanks for your help. This has raised another question for me though.

My understanding is that const animals = [‘Hen’, ‘elephant’, ‘llama’] these are my arrays with the name of the variable being animals.

So animal => this is the name of the function and the code to pass it as a function.

I don’t understand how I would pull through the first letter of animals using return animal[0] as animal is my function name.

1 Like

Those are the elements of the array. The name refers to the array, not the elements.

The function is anonymous and has no name, nor any need of one. animal is known as the function parameter. Since map is an iterator, it will iterate the object array, animals and perform an action on each element, animal.


Let’s examine the map method using ES5 syntax…

words.map(function (word) {
    return word.charAt(0)
})

Note there is no name given to the function (which is what anonymous means).

The new arrow syntax eliminates the function keyword but keeps the parameter container. When there is exactly one parameter we are permitted to drop the parentheses. We can also leave off the curly braces and the return keyword if our function body contains only one statement or expression.

3 Likes

Thanks for that. I see where I went wrong with my understanding.

2 Likes

Hey @mtf, I read above that .map() refers to First Letter of string as String itself acts as Array. But if that’s true, why it’s not Upper Casing only first letters of one, two etc. Why does it upper case entire word then? These are also strings. Please clarify if I am missing something.

go read what it does
google.com/search?q=mdn+array+map

words = ['one', 'two', 'three', 'four', 'five']
 upcaseWords = words.map(word => word[0].toUpperCase())

Result

["One", "Two", "Three", "Four", "Five"]

Note the slight difference.

1 Like

hello everyone
i didnt understand so much what do i need to do in the The .map() Method exersie.
i the first step they askes us to write a code that create a new array that contains the first character of each string in the animals array. i wrote

const secretMessage = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(animal => animal * 9);

i did exatully what they ask for and i was incorrect and dont understand why.
if someone can explain me the exercise better ill loved to.
thankes.

1 Like

So we have an animals array:

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

and we need to get the first letter of each animal. So we need the H of hen, the e of elephant and so forth

you could do this process without any code, so you know what the secret message is

Could you explain to me how this:

const secretMessage = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(animal => animal * 9);

would get the first letter of each animal in the animals array? And how this decrypt the secret message?

1 Like

I’m curious why my code is not registering as a valid answer. the console reads “HelloWorld”, as expected.

const secretMessage = animals.map(animal => {
  return animal[0].split('');
});

Please advise, thank you!

using split will add an array to secretMessage array.

which we can see when we do:

console.log(secretMessage);
1 Like

A post was split to a new topic: Missing initialiser

I’ve been trying to do this lesson but I am confused why the colors of the words are very different from the example images when I write the exact same thing. I can’t imagine what I would be doing wrong that it doesn’t match.

Can we see your code?

For example, this code, copypasted exactly from the example. It displays very differently in the window, for a reason I can’t figure out.

const bigNumbers = numbers.map(number => {
  return number * 10;
});

(I did pass the exercise, but it’s a bit confusing when things don’t display as you are told they will.)

1 Like

The colors in the codecademy editor are not always even reliable. I wouldn’t worry about it.

2 Likes

Alright, then, thanks!

Hi

My code not working when i am using arrow function can someone please check why?

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

const secretMessage  = animals.map( item =>
 return item[0];
);

// Following below code is working

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

const secretMessage  = animals.map(function(char) {
   return char[0];
 } 
 );

From the following topic
.map() lesson

Thanks

If we look at the basic syntax:

(param1, param2, …, paramN) => { statements } 
(param1, param2, …, paramN) => expression
// equivalent to: => { return expression; }

// Parentheses are optional when there's only one parameter name:
(singleParam) => { statements }
singleParam => { statements }

// The parameter list for a function with no parameters should be written with a pair of parentheses.
() => { statements }

we can see the shorthand you attempt to use:

(param1, param2, …, paramN) => expression

see? No return keyword used. When omitting the curly brackets, the return keyword also has to be left out. The return becomes implicit.