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


#1

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

similar-but-different

I am specifically asking about the .map question…

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]?


#2

.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?


#3

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


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

#5

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.


#6

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


#7

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?


#8

That is the function. It is introduced in ES6.

The ES5 equivalent is,

function (x) {
    return x[0];
}

#9
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


#10

can you include the exercise url?

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


#11

I have now edited the prior message with the url.


#12

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('');

#13

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


#14

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


#15

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

Example again…

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

#16

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.


#17

what would you suggest that I put as a parameter?


#18

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?


#19

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.


#20

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