freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../basic-javascript/use-bracket-notation-to-fin...

81 lines
1.8 KiB
Markdown
Raw Normal View History

---
id: bd7123c9c452eddfaeb5bdef
title: Use Bracket Notation to Find the Nth-to-Last Character in a String
challengeType: 1
videoUrl: 'https://scrimba.com/c/cw4vkh9'
forumTopicId: 18344
---
## 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>
</section>
## Instructions
<section id='instructions'>
Use <dfn>bracket notation</dfn> to find the second-to-last character in the <code>lastName</code> string.
<strong>Hint</strong><br>Try looking at the <code>thirdToLastLetterOfFirstName</code> variable declaration if you get stuck.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>secondToLastLetterOfLastName</code> should be "c".
testString: assert(secondToLastLetterOfLastName === 'c');
- text: You have to use <code>.length</code> to get the second last letter.
testString: assert(code.match(/\.length/g).length === 2);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Example
var firstName = "Ada";
var thirdToLastLetterOfFirstName = firstName[firstName.length - 3];
// Setup
var lastName = "Lovelace";
// Only change code below this line
var secondToLastLetterOfLastName = lastName;
```
</div>
### After Test
<div id='js-teardown'>
```js
(function(v){return v;})(secondToLastLetterOfLastName);
```
</div>
</section>
## Solution
<section id='solution'>
```js
var firstName = "Ada";
var thirdToLastLetterOfFirstName = firstName[firstName.length - 3];
var lastName = "Lovelace";
var secondToLastLetterOfLastName = lastName[lastName.length - 2];
```
</section>