2018-09-30 23:01:58 +01:00
---
id: bd7123c9c452eddfaeb5bdef
title: Use Bracket Notation to Find the Nth-to-Last Character in a String
challengeType: 1
2020-05-21 17:31:25 +02:00
isHidden: false
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cw4vkh9'
2019-07-31 11:32:23 -07:00
forumTopicId: 18344
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
You can use the same principle we just used to retrieve the last character in a string to retrieve the Nth-to-last character.
For example, you can get the value of the third-to-last letter of the <code>var firstName = "Charles"</code> string by using <code>firstName[firstName.length - 3]</code>
2020-03-25 08:07:13 -07:00
Example:
```js
var firstName = "Charles";
var thirdToLastLetter = firstName[firstName.length - 3]; // thirdToLastLetter is "l"
```
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
Use <dfn>bracket notation</dfn> to find the second-to-last character in the <code>lastName</code> string.
2020-03-25 08:07:13 -07:00
<strong>Hint: </strong> Try looking at the example above if you get stuck.
2018-09-30 23:01:58 +01:00
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: <code>secondToLastLetterOfLastName</code> should be "c".
2019-07-13 00:07:53 -07:00
testString: assert(secondToLastLetterOfLastName === 'c');
2019-11-27 02:57:38 -08:00
- text: You should use <code>.length</code> to get the second last letter.
2020-03-25 08:07:13 -07:00
testString: assert(code.match(/\.length/g).length > 0);
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Setup
var lastName = "Lovelace";
// Only change code below this line
2020-03-25 08:07:13 -07:00
var secondToLastLetterOfLastName = lastName; // Change this line
2018-09-30 23:01:58 +01:00
```
</div>
### After Test
<div id='js-teardown'>
```js
2018-10-20 21:02:47 +03:00
(function(v){return v;})(secondToLastLetterOfLastName);
2018-09-30 23:01:58 +01:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
var lastName = "Lovelace";
var secondToLastLetterOfLastName = lastName[lastName.length - 2];
```
</section>