Lesson 23


#1

Using substring to find "Jan“ from ”January“
I typed the console.log("January".substring(0,2)); They said it's wrong. The answer is (0,3)
I just confused "J" is position 0, "n" is position 2. Why it needs from (0,3) not (0,2)?


#2

In substring(), index start is included in the result, but index end is not.
So, if "January" looks like this

[0][1][2][3][4][5][6]
[J][a][n][u][a][r][y]

Now if you use substring(0, 2), you will take take characters from positions 0 and 1 and stop at 2, without including it. So your result would be Ja. substring() takes positions where to start, 0, and what to omit, 2. So to take character from position 2, we need to go to that position and tell our substring() to stop at it +1. That's why you need to call it with (0, 3) to take first 3 characters.