# How do you return the first letter of different strings in an array?

I would like to refer to a previous exercise for this question.

similar-but-different

Below is the work I have done (below create below):

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

// Create the secretMessage array below
let SecretMessage = I dont know what goes here
``````

I’ve seen `.filter` being used and `word[0]` and `.join` but a basic example of how to use `word[0]` with a .map statement would be great.

Here is an example of a .map function if you forgot:

``````let numbers = [1, 2, 3, 4, 5];

let bigNumbers = numbers.map(function(number) {
return number * 10;
});
``````

Hopefully I’ve provided enough context and plenty of examples. Remember I’m specifically asking: how do I find the first letter of multiple strings with different letters in a .map statement using `word[0]`?

`.map()` provides us with a way to access the different elements of an array, manipulate this element and store the result in a list

that is exactly what we need to do, i don’t see a question mark anywhere in your question?

I’ve edited the question to ask the following question:
how do you use word[0] in a .map command?

1 Like
``````let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
first_letters = animals.map(x => x[0]);
``````
1 Like

lets look at the example:

``````let numbers = [1, 2, 3, 4, 5];

let bigNumbers = numbers.map(function(number) {
return number * 10;
});
``````

map is looping over each element in array, in each iteration of the loop, the element of the list is stored in `number`, then we can manipulate number, which is then added to the newly constructed array.

could first_letters have another name? For example, SecretMessage could be it’s name, correct?

I just don’t see what part this plays with the .map command - it doesnt include the function part.

Here is the function part:

``````let numbers = [1, 2, 3, 4, 5];

let bigNumbers = numbers.map(function(number) {
return number * 10;
});
``````

Are you missing a part of the equation?

That is the function. It is introduced in ES6.

The ES5 equivalent is,

``````function (x) {
return x[0];
}``````
2 Likes
``````let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
let SecretMessage = animals.map(x => x[0]);
``````

This is what I put…it is throwing an error.
What do I need to fix? I’ve been scouring MDN all day haha…

exercise

can you include the exercise url?

the exercise might ask you to first to write the full function before using the shorthand

I have now edited the prior message with the url.

If you want a single string then you will need to join the array elements. `map` returns an array.

``let SecretMessage = animals.map(x => x[0]).join('');``

if you look at the instructions of step 3:

Refactor the method call from Step 1 using arrow function syntax.

so first you have to write the full function in step 1 before you can write the arrow shorthand in step 3

I’m not sure how to write the full function, that is the problem. How do I write the full function?

For example, what goes in the function slot in step 1?

Example again…

``````let bigNumbers = numbers.map(function(number) {
return number * 10;
});
``````

big numbers is without the arrow syntax shorthand:

``````let numbers = [1, 2, 3, 4, 5];

let bigNumbers = numbers.map(function(number) {
return number * 10;
});
``````

so you can get inspiration from there. Now we have to convert bigNumbers to make it work for secretMessage.

what would you suggest that I put as a parameter?

You are free to choice parameter name, but pick something logic and descriptive. Maybe the lesson even has a suggestion for a parameter name? Such a small question, given how long you have been programming, you should know this?

I have been working on this problem this evening for 3 hours. Earlier I was at a bootcamp also doing problem solving, so I have a better idea of how to debug a little bit more. I dont understand how to use the parameter in the function body itself.

1 Like

Even an unrelated example would be helpful as I am confused.

1 Like