Simulation Example

Here is how the previous code might be rewritten in the context of a simulation game:

let playerName = 'John';

// Use square bracket notation to access the second character (i.e. "o")
if (playerName[1] === 'o') {
  console.log('The second letter of your name is "o"');
} else {
  console.log('The second letter of your name is not "o"');
}

// Use square bracket notation to access the last character (i.e. "n")
if (playerName[playerName.length - 1] === 'n') {
  console.log('The last letter of your name is "n"');
} else {
  console.log('The last letter of your name is not "n"');
}

This code would output:

The second letter of your name is "o"
The last letter of your name is "n"

Last updated