Dont understand this piece of code

got stuck on the last of the three questions in this lesson (under making new strings from new parts), the specific part i dont understand is the ‘var name’ in the solution. i dont see it being required in the five steps above, and i dont remember learning it in the lesson above.

Are you struggling with a lesson on FreeCodeCamp or a lesson on MDN?

If a lesson on FreeCodeCamp, what is the lesson name?

Hi @ibnadam,

I have not looked through the entire lesson but I can tell you how they got the var name bit.

In the instructions, they suggest that you break the string from the semi-colon and then get the name of the city that follows. Here’s how it works:

  1. You find the index of the semicolon first, this will be your start point or index i.e. where you’re going to begin cutting the string.

  2. You get the substring that contains the name only of the city by cutting the entire string from the semicolon on. If you look at the documentation on String.prototype.slice();, you can see that providing an ending index is optional. If you don’t, the method by default returns the entire string from the start point (included) to the end of the string.

  3. Why the +1? Well, because if you remove the +1, you’re including the semicolon in the substring. That’s not what we want, so we have to add 1 to the start index. For example, you’d get ‘;Manchester City…etc’ if you remove the + 1.

I hope this helps.