2021-06-15 07:49:18 +00:00
|
|
|
---
|
|
|
|
id: bd7123c9c450eddfaeb5bdef
|
2021-07-21 15:23:20 +00:00
|
|
|
title: Usar notação de colchetes para encontrar o enésimo caractere em uma string
|
2021-06-15 07:49:18 +00:00
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cWPVJua'
|
|
|
|
forumTopicId: 18343
|
|
|
|
dashedName: use-bracket-notation-to-find-the-nth-character-in-a-string
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-07-14 15:32:51 +00:00
|
|
|
Você também pode usar <dfn>notação de colchetes</dfn> para pegar caracteres em outras posições em uma string.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
2021-07-29 16:41:44 +00:00
|
|
|
Lembre-se de que computadores começam contando do `0`. Então, o primeiro caractere é na verdade o caractere na posição 0.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
2021-07-14 15:32:51 +00:00
|
|
|
Exemplo:
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const firstName = "Ada";
|
|
|
|
const secondLetterOfFirstName = firstName[1];
|
2021-06-15 07:49:18 +00:00
|
|
|
```
|
|
|
|
|
2021-07-14 15:32:51 +00:00
|
|
|
`secondLetterOfFirstName` teria o valor da string `d`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-07-14 15:32:51 +00:00
|
|
|
Vamos tentar definir `thirdLetterOfLastName` para ser igual a terceira letra da variável `lastName` usando notação de colchetes.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
2021-07-29 16:41:44 +00:00
|
|
|
**Dica:** tente olhar o exemplo acima se você ficar travado.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-07-14 15:32:51 +00:00
|
|
|
A variável `thirdLetterOfLastName` deve ter o valor de `v`.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(thirdLetterOfLastName === 'v');
|
|
|
|
```
|
|
|
|
|
2021-07-14 15:32:51 +00:00
|
|
|
Você deve usar notação de colchetes.
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(v){return v;})(thirdLetterOfLastName);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
2021-10-27 15:10:57 +00:00
|
|
|
const lastName = "Lovelace";
|
2021-06-15 07:49:18 +00:00
|
|
|
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
const thirdLetterOfLastName = lastName; // Change this line
|
2021-06-15 07:49:18 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const lastName = "Lovelace";
|
|
|
|
const thirdLetterOfLastName = lastName[2];
|
2021-06-15 07:49:18 +00:00
|
|
|
```
|