Im wonder why is code using length property in order to find any character from the last one? Im talking about challenge down bellow:
The challenge is specifically asking about the nth character from the end. If a string is 4 characters long, then the second to last character will be at index 2. If the string is 178 characters long, the second to last character will be at index 176
. We don’t want to hard code the length of the string in, so we let the computer do the counting for us. The last character of a string someString
will always be someString.length - 1
no matter how long the string is. The second to last character will be someString.length - 2
, the third someString.length - 3
, and so on.
1 Like
Ok i understand now, thank you both.