Skip to content

Instantly share code, notes, and snippets.

@divya-kanak
Last active January 16, 2020 08:10
Show Gist options
  • Save divya-kanak/79792fbc97063bb60bf246972ce42b72 to your computer and use it in GitHub Desktop.
Save divya-kanak/79792fbc97063bb60bf246972ce42b72 to your computer and use it in GitHub Desktop.
Use case of .slice() and concatenation?
<!DOCTYPE html>
<html>
<body>
<h2>Your solution:(without +1)</h2>
<div class='output'>
<ul>
</ul>
</div>
<hr>
<h2>Tutorial solution:(with +1)</h2>
<h5>Here, +1 is needed for select the character after the semicolon(;). That means your station Name should be (Manchester Piccadilly) instead of ( ;Manchester Piccadilly).</h5>
<div class='tutorial_output'>
<ul>
</ul>
</div>
</body>
</html>
const list = document.querySelector('.output ul');
list.innerHTML = '';
let stations1 = ['MAN675847583748sjt567654;Manchester Piccadilly',
'GNF576746573fhdg4737dh4;Greenfield',
'LIV5hg65hd737456236dch46dg4;Liverpool Lime Street',
'SYB4f65hf75f736463;Stalybridge',
'HUD5767ghtyfyr4536dh45dg45dg3;Huddersfield'];
for (let i = 0; i < stations1.length; i++) {
let input = stations1[i];
let stationAbbr = input.slice(0,3);
let semicolon = input.indexOf(';');
let stationName = input.slice(semicolon);
let station = stationAbbr + ': ' + stationName;
let listItem = document.createElement('li');
listItem.textContent = station;
list.appendChild(listItem);
}
const tutorial_output_list = document.querySelector('.tutorial_output ul');
tutorial_output_list.innerHTML = '';
for (let i = 0; i < stations1.length; i++) {
let input = stations1[i];
let stationAbbr = input.slice(0,3);
let semicolon = input.indexOf(';');
let stationName = input.slice(semicolon+1);
let station = stationAbbr + ': ' + stationName;
let listItem = document.createElement('li');
listItem.textContent = station;
tutorial_output_list.appendChild(listItem);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment