HELP plz understanding JavaScript issue

 const list = document.querySelector('.output ul');
list.innerHTML = '';
let stations = ['MAN675847583748sjt567654;Manchester Piccadilly',
                'GNF576746573fhdg4737dh4;Greenfield',
                'LIV5hg65hd737456236dch46dg4;Liverpool Lime Street',
                'SYB4f65hf75f736463;Stalybridge',
                'HUD5767ghtyfyr4536dh45dg45dg3;Huddersfield'];

for (let i = 0; i < stations.length; i++) {
  let input = stations[i];
  let code = input.slice(0,3);
  let semiC = input.indexOf(';');
  let name = input.slice(semiC + 1); //This line!
  let result = code + ': ' + name;
  let listItem = document.createElement('li');
  listItem.textContent = result;
  list.appendChild(listItem);
}

can u tell me what is that +1 for inside the name variable?

Hello @code-hunter

slice here will take copy of the string from start till the index of the ; but since slice use exclusive way that it does not take the last index so they add 1 so it include that ;

check this one to get more about slice

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

hope that help and have a nice day :slight_smile:

1 Like

Hey, Thank you so much! never knew i could do that.

you very welcome :slight_smile:

1 Like