2021-06-15 00:49:18 -07:00
|
|
|
---
|
|
|
|
id: bd7123c9c451eddfaeb5bdef
|
2021-07-21 20:53:20 +05:30
|
|
|
title: Usar notação de colchetes para encontrar o último caractere em uma string
|
2021-06-15 00:49:18 -07:00
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cBZQGcv'
|
|
|
|
forumTopicId: 18342
|
|
|
|
dashedName: use-bracket-notation-to-find-the-last-character-in-a-string
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
Para pegar a última letra de uma string, você pode subtrair um do tamanho da string.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
2021-11-03 08:22:32 -07:00
|
|
|
Por exemplo, se `const firstName = "Ada"`, você pode pegar o valor da última letra da string ao usar `firstName[firstName.length - 1]`.
|
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 = "Ada";
|
|
|
|
const lastLetter = firstName[firstName.length - 1];
|
2021-06-15 00:49:18 -07:00
|
|
|
```
|
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
`lastLetter` teria o valor da string `a`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-07-30 01:41:44 +09:00
|
|
|
Use <dfn>notação de colchetes</dfn> para descobrir o último caractere na variável `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
|
|
|
`lastLetterOfLastName` deve ser a letra `e`.
|
2021-06-15 00:49:18 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(lastLetterOfLastName === 'e');
|
|
|
|
```
|
|
|
|
|
2021-07-14 21:02:51 +05:30
|
|
|
Você deve usar `.length` para pegar a ú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;})(lastLetterOfLastName);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --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 lastLetterOfLastName = 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 lastLetterOfLastName = lastName[lastName.length - 1];
|
2021-06-15 00:49:18 -07:00
|
|
|
```
|