Using.map


#1

Hello!

I’m having trouble finding the iterator section in the help category, so I changed the category to function which is sort of true.
Im trying to get clarification on how to use .map in the animals example
Here is my code:

There is an extra token that I cannot get rid of.
The code:

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]; 
});

Any help would be appreciated. I know it’s about doing it on your own, but I dont know what I’m doing wrong.


#2

the code you posted here seems to be fine, what is the question?


#3
let secretMessage = animals.map(function(animal) {
  return animal.charAt(0);});

The above is in regular function syntax (not arrow function syntax). What exactly is .map doing here?

On the animals exercise it pulls up hello world.


#4

.map() is a method which allows us to to loop over an array and creating a new array in the process, if we have to write it manually, it would look something like this:

let animals = ['Hen', 'elephant', 'llama', 'leopard', 'ostrich', 'Whale', 'octopus', 'rabbit', 'lion', 'dog'];
secretMessage = [];
for (let i = 0; i < animals.length; i++){
    animal = animals[i];
    secretMessage.push(animal);
}

except that .map() is function, which is why we can use return to add elements to the array (the function handles this for us, so we don’t have to use .push()

as you see, the map() code is much cleaner


#5

I know youre responding, but I just wanted to make sure I know what .map does if you have an array. In this case, shouldnt it pull the first letter of each animal?


#6

it does, but map also creates an array (its part of the map function his functionality), with each letter of the animals, deciphering the secret message.


#7

is the hello world at the end then a string?


#8

or is the hello world an array at the end? normally prototype.push takes a string to an array, correct?


#9

no, if you check the documentation on .map() you will see that it returns an array.

we could convert it to string using .join() to display the deciphered nicely to the end user of our program


#10

could you make .map choose items from a list going from the back to the front?


#11

very unlikely, why not check the map documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

learning to find and read the right documentation is something you should get used, given you have been programming for a while now


#12

I read the documentation and various folks said that it acts very similar to a for loop, thats why I asked. But yes, I am definitely working on that skill. And the documentation only talks about using it with numbers, which isnt applicable in most situations, unless you’re working with numbers.


#13

yes, but the documentation says:

map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results

if it says in order, it means sequential order. Which means there is no changing. It might be similair to a for loop, doesn’t mean its behaviour is identical.


#14

I know I’m making mistakes but I’m still learning javascript.


#15

The .map exercise

let bigNumbers = [100, 200, 300, 400, 500];\
let smallNumbers = bigNumbers.map(numbers => numbers/10);

This is now the second part. I used arrow function syntax and am still getting a syntax error.

I know I need to do it myself but I first have to get used to the mistakes I’m making. Im trying it out now. I think it was a syntax error.


#16

thats what learning is initially…alot of failing!


#17

okay, so what do i have to do with it?

For the fact that you have been on the forum for a while, its surprising that you still do not format your code:

How do I format code in my posts?

this will ensure indent shows up correctly, as well as quotation marks and apostrophe

if you provide the exercise url, we can access the exercise more quickly, making it better for us to help you, so you get a better reply


#18

let bigNumbers = [100, 200, 300, 400, 500];
let smallNumbers = bigNumbers.map(function(n) {return n / 100;})

I figured it out :slight_smile:


#19