Help needed - Use Bracket Notation to Find the Last Character in a String

Help needed - Use Bracket Notation to Find the Last Character in a String
0

#1

Tell us what’s happening:

I have managed to do resolve this challenge but I need someone to give me the breakdown of how it happened. thanks

Your code so far


// Example
var firstName = "Ada";
var lastLetterOfFirstName = firstName[firstName.length - 1];

// Setup
var lastName = "Lovelace";

// Only change code below this line.
var lastLetterOfLastName = lastName[lastName.length - 1];


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:62.0) Gecko/20100101 Firefox/62.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-last-character-in-a-string


#2

you have a string stored in the variable lastName
the string is Lovelace

To access the characters of the string , you use something called bracket notation
for eg.
lastName[0]
this will give us the character at location zero which is L
lastName[1]
this will give us the character at location one which is o
etc.

If you don’t know how long your string is and you want to access the last character you need to ask first about the length.
For eg. Lovelace has a length of 8
so to get the last letter e we should write
lastName[8-1] as seven is the eighth position (remember that zero is the first position)

And in order to write ‘7’ without actually counting the length of each string , we let the computer do it like this
lastName[lastName.length - 1]

hope this helps


#3

wow, that’s the best explanation ever!

thanks a ton!