FAQ: Iterators - The .map() Method

The exercise has:

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

and if you don’t use an arrow function. you could do

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

and without .map, you could do:

function getFirstLetter(animal) {
  return animal[0];
}
const secretMessage = [];
for (let an of animals) {
  secretMessage.push(getFirstLetter(an));
}

I don’t really understand this. So in this case num has to be num specifically or can it be anything else? I mean can I use another word (like number, cat etc) instead of num?

A variable name is a label, so yes, it can be anything you like. Suggest keep it sensible and object oriented. Describe the contents with as brief a name as one can. Remember, variables are not values, they refer to values.

By sensible I mean, do we need,

const number_of_cats_alive_in_schrodingers_box = undefined;

?

That helps! Thanks a lot!

1 Like

You’re welcome. Okay, now consider,

let a = 6
const b = 7

a and b are variables, one of which is set to constant. They both refer to values that are somewhere in memory.

a = b

What happens to a?

a becomes b, so now a = 7?

Bingo! a now refers to the same object that b refers to. Is a the same as b?

Looks like that, since a is let variable, so until it changes its value later in the code a equals b.

ps JS makes me wanna cry, I have never missed CSS more :sweat_smile:

1 Like

hi ,
const animals = [‘Hen’, ‘elephant’, ‘llama’, ‘leopard’, ‘ostrich’, ‘Whale’, ‘octopus’, ‘rabbit’, ‘lion’, ‘dog’];
const secretMessage = animals.map(first => {

return first[0]

})
im confused as to why, first[0] , in this situation gives the first letter of every word in the array and not, ‘Hen’ , if i console.log(animals.length) the length of the array is 10. wouldnt that put ‘Hen’ at first[0]? idk .length is counting the elements in the array. how does .map() knwo to grab the first letter of all the elements with first[0]? thank you

first is, in turn, ‘Hen’, then, ‘elephant’, then, ‘llama’, and so on. So first[0] is the first letter of each word.

1 Like

Yeah, you can declare a function beforehand and then pass it in map or forEach.

Can someone please help me understand why this seems to work:

const bigNumbers = [100, 200, 300, 400, 500];
const smallNumbers = bigNumbers.map(num => {return num / 100;});

While the following does not:

const bigNumbers = [100, 200, 300, 400, 500];
const smallNumbers = bigNumbers.map(num => num / 100;);

From what I understand about arrow functions, I shouldn’t need to explicitly state return or surround the function body in brackets. What am I missing?

Uncaught SyntaxError: missing ) after argument list

This is in Chrome console:

 > bigNumbers.map(num => num / 100)
   (5) [1, 2, 3, 4, 5]
 > 
1 Like

Sometimes a second set of eyes is the “solution.” Thanks!

1 Like

Hi,
I wrote : -
const secretMessage = animals.map(animal => animal[0]); and it worked but when I rewrote this as : const secretMessage = animals.map(animal => animal.at(0)); it gave the following error : -
TypeError: animal.at is not a function at animals.map.animal

I expected the String function at() to work since the callback function was having String object instances as input. Can someone kindly explain where I am going wrong here. Thanks in advance.

Regards
Prabir Sarkar

Unless one is wrong (it happens), .at() is an array method. Not sure it applies to strings, but I’ll leave that for you to look up on MDN.

 > animals.map(animal => [...animal].at(0))
<-  ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
 > 

Do please look for the String documentation. As it turns out, .at() must also be a String method:

 > animals.map(animal => animal.at(0))
<-  ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']
 >

Thanks. Yes, it is a String function as you also pointed out. Ran it on the node REPL

Getting the same error.

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

javascript - Why am I getting “.at” is not a function? - Stack Overflow

2 Likes