2021-06-15 00:49:18 -07:00
---
id: bd7123c9c452eddfaeb5bdef
2021-07-21 20:53:20 +05:30
title: Usar notação de colchetes para descobrir o enésimo caractere antes do último em uma string
2021-06-15 00:49:18 -07:00
challengeType: 1
videoUrl: 'https://scrimba.com/c/cw4vkh9'
forumTopicId: 18344
dashedName: use-bracket-notation-to-find-the-nth-to-last-character-in-a-string
---
# --description--
2021-07-30 01:41:44 +09:00
Você pode usar o mesmo princípio que nós acabamos de usar para recuperar o último caractere em uma string, para recuperar o enésimo caractere antes do último caractere.
2021-06-15 00:49:18 -07:00
2021-11-03 08:22:32 -07:00
Por exemplo, você pode pegar o valor da antepenúltima letra da string `const firstName = "Augusta"` usando `firstName[firstName.length - 3]`
2021-06-15 00:49:18 -07:00
2021-07-14 21:02:51 +05:30
Exemplo:
2021-06-15 00:49:18 -07:00
```js
2021-11-03 08:22:32 -07:00
const firstName = "Augusta";
const thirdToLastLetter = firstName[firstName.length - 3];
2021-06-15 00:49:18 -07:00
```
2021-07-14 21:02:51 +05:30
`thirdToLastLetter` teria o valor da string `s` .
2021-06-15 00:49:18 -07:00
# --instructions--
2021-07-14 21:02:51 +05:30
Use < dfn > notação de colchetes</ dfn > para descobrir o penúltimo caractere na string `lastName` .
2021-06-15 00:49:18 -07:00
2021-07-30 01:41:44 +09:00
**Dica:** tente olhar o exemplo acima se você ficar travado.
2021-06-15 00:49:18 -07:00
# --hints--
2021-07-14 21:02:51 +05:30
`secondToLastLetterOfLastName` deve ser a letra `c` .
2021-06-15 00:49:18 -07:00
```js
assert(secondToLastLetterOfLastName === 'c');
```
2021-07-14 21:02:51 +05:30
Você deve usar `.length` para pegar a penúltima letra.
2021-06-15 00:49:18 -07:00
```js
assert(code.match(/\.length/g).length > 0);
```
# --seed--
## --after-user-code--
```js
(function(v){return v;})(secondToLastLetterOfLastName);
```
## --seed-contents--
```js
// Setup
2021-11-03 08:22:32 -07:00
const lastName = "Lovelace";
2021-06-15 00:49:18 -07:00
// Only change code below this line
2021-11-03 08:22:32 -07:00
const secondToLastLetterOfLastName = lastName; // Change this line
2021-06-15 00:49:18 -07:00
```
# --solutions--
```js
2021-11-03 08:22:32 -07:00
const lastName = "Lovelace";
const secondToLastLetterOfLastName = lastName[lastName.length - 2];
2021-06-15 00:49:18 -07:00
```