2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: bd7123c9c450eddfaeb5bdef
|
|
|
|
title: Use Bracket Notation to Find the Nth Character in a String
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/cWPVJua'
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 18343
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
You can also use <dfn>bracket notation</dfn> to get the character at other positions within a string.
|
|
|
|
Remember that computers start counting at <code>0</code>, so the first character is actually the zeroth character.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
Let's try to set <code>thirdLetterOfLastName</code> to equal the third letter of the <code>lastName</code> variable using bracket notation.
|
|
|
|
<strong>Hint</strong><br>Try looking at the <code>secondLetterOfFirstName</code> variable declaration if you get stuck.
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
tests:
|
|
|
|
- text: The <code>thirdLetterOfLastName</code> variable should have the value of <code>v</code>.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert(thirdLetterOfLastName === 'v');
|
2018-10-04 14:37:37 +01:00
|
|
|
- text: You should use bracket notation.
|
2019-07-13 00:07:53 -07:00
|
|
|
testString: assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Example
|
|
|
|
var firstName = "Ada";
|
|
|
|
var secondLetterOfFirstName = firstName[1];
|
|
|
|
|
|
|
|
// Setup
|
|
|
|
var lastName = "Lovelace";
|
|
|
|
|
2020-03-02 23:18:30 -08:00
|
|
|
// Only change code below this line
|
2018-09-30 23:01:58 +01:00
|
|
|
var thirdLetterOfLastName = lastName;
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2018-10-20 21:02:47 +03:00
|
|
|
(function(v){return v;})(thirdLetterOfLastName);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
var lastName = "Lovelace";
|
|
|
|
var thirdLetterOfLastName = lastName[2];
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|