Learning JavaScript: Iterators


#1

How do you know to use .charAt(0)?
‘’‘let secretMessage = animals.map(animal => animal.charAt(0));’’’


#2

Quite possibly you don’t. But at this point we do know how to use str[0] to represent the first index position of a string. If the String.charAt() method is new to you, then consider your education augmented that slight bit. Add it to your library. Why look a gift horse in the mouth?


#3

Is there a library we can access? (for this information). And I don’t remember using str[0] (my memory could be off). Thanks for your help.


#4

You’d look at what you’re trying to do - get a specific character from a string, and then you’d look up what behaviour is supported by string (and looking up a character by position sounds like something that would exist)

google “mdn string” ->


#5

At the start of the track there is a link in one of the earlier lessons that points to Mozilla Developer Network. Go back and find that link, or bookmark the one above and keep it nearby at all times. Don’t just race through a lesson. You’ll never remember anything that way. Stop and do the reading and research that would accompany the new concepts in the lesson.

str[0]

has been explained in early lessons on strings and also in lessons on arrays.

myString = "The start of a new day"
console.log(myString.length)
// 22
console.log(myString[22])
// undefined
console.log(myString[myString.length - 1])
// y
console.log(myString[0])
// T

We’re at a point in the course where it is anticipated all the elementary concepts are behind us now and we are ready to push ahead into advanced concepts. If you have scrambled through the units without stopping to review, then you will find yourself right back where you started… The beginning.

Now would be a good time to dig deep into the subject of String objects (and Array objects) and suss out all of the available methods inherited from their constructor prototype.

Take for instance String.slice()

console.log(myString.slice(-1))
// y
console.log(myString.slice(0, 1))
// T

And the ever popular, String.indexOf()

console.log(myString.indexOf("T"))
// 0
console.log(myString.indexOf('y'))
// 21
console.log(myString.indexOf('day'))
// 19

Whatever you do, don’t put the course in your rearview mirror until you can walk through every exercise in your sleep. That is the burden we take upon ourselves as self-motivated learners. There is nobody looking over our shoulder to correct our errant ways. All the more reason to do extensive reading on every topic, else one day you may find yourself caught with your trousers down.