Learn JavaScript iterator .map()


#1

can someone explain what I’m supposed to do here?

Just getting the code does not teach me anything, and the examples and the hint seem completely unrelated to the code that is already there. WTH am I supposed to do?
the hint says return, but the code that is given does not even contain the use of return!
Why are we not given a step by step show of the code, in order to learn? There’s just so many bad things about this particular excercise!

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

// Create the secretMessage array below
let secretMessage = animals.map(function(animal){
return animal[0]
console.log(secretMessage.join(’’));

})


#2

which exercise does this involve? Can you share the url


#3

https://www.codecademy.com/courses/learn-javascript-iterators/lessons/javascript-iterators/exercises/map?action=lesson_resume&course_redirect=introduction-to-javascript


#4

this is the code given:

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

// Create the secretMessage array below


console.log(secretMessage.join(''));

let bigNumbers = [100, 200, 300, 400, 500];

// Create the smallNumbers array below

is there anything in this code which you have learned yet?

The thing with this exercise is that we are already given a program, which we need to extend. So the instructions and hint refer to extending of the program, not the code that is already there.

This exercise really focuses on teaching map(), so it provides you with things you already learned, to focus on learning a new thing (in this case map() function)


#5

I know what an array is and I know the console.log and the join.

What I needed was to be shown how to use the charAt() or something
I know that map() creates a new array, but there were nothing to tell me how to extract the first character in each string, and how to put them into the new array.

The examples shown and the hint did not tell me anything useful either :confused:

with “just getting the code” I mean when the pop up wants to help me by giving me the finished code, which shows the code after ALL the exercises are finished. What I need is the code from ONLY the first step to see how they did it.


#6

for the fact that you say you don’t know how to do all these things, you got pretty close, this is really close:


let secretMessage = animals.map(function(animal){
return animal[0]
console.log(secretMessage.join(''));

})

animal will contain (in order) each string within the array, so doing animal[0] will get the first letter of each animal. Using square brackets and indexes to get values from strings/arrays has been taught?

map() creates the new array, which you store in secretMessage variable

so all you need to do is after the function, log the secret message:

console.log(secretMessage.join(''));

so this line should be after the function, not inside the function body.


#7

You can use the following code to return the first element of a string:

return word[0]

That seems useful enough. Does Get Code give a solution with String.charAt(0)?


#8

Thank you! I have an exam next Thursday on html, css and JavaScript so I guess I’m a little stressed out :sweat_smile:

but I do feel there is something about this particular lesson that makes it hard. I didn’t have any problems this big with any of the other.
Thanks again


#9

yes, charAt. No return there.
But I think I get it now. It might seem useful to you but the usefulness for me was none at the time it was given to me :slight_smile: But it’s ok now.


#10

It will be useful to know that arrow function notation offers us some shortcuts, but they are not universal. For instance we can write code on one line that is completely foreign to ES5…

Array.map(x => x.charAt(0));

Notice, no parens on the parameter, no curly braces, and no return keyword.


#11

yes it does:

let secretMessage = animals.map(animal => animal.charAt(0));

@sirimg, looking at charAt documentation:

we can see its similair to accessing by index using square brackets

the confusing thing is that the solution is using a shorthand in which return isn’t required. Its a shorthand es6 offers.


#12

I think its just too many shorthands and making stuff easy for more experienced people.
I only want to understand the very basic of js to write some simple code. I will just skip the ones where nothing makes sense.


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.