2021-02-06 04:42:36 +00:00
|
|
|
---
|
|
|
|
id: bd7123c9c451eddfaeb5bdef
|
2021-03-15 14:01:16 -06:00
|
|
|
title: Utiliza la notación de corchetes para encontrar el último carácter en una cadena
|
2021-02-06 04:42:36 +00: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-03-15 14:01:16 -06:00
|
|
|
Con el fin de obtener la última letra de una cadena, puedes restar uno a la longitud del texto.
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-10-31 23:08:44 -07:00
|
|
|
Por ejemplo, sí `const firstName = "Ada"`, puedes obtener el valor de la última letra de la cadena usando `firstName[firstName.length - 1]`.
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-02-21 08:49:16 -07:00
|
|
|
Ejemplo:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
2021-10-31 23:08:44 -07:00
|
|
|
const firstName = "Ada";
|
|
|
|
const lastLetter = firstName[firstName.length - 1];
|
2021-02-06 04:42:36 +00:00
|
|
|
```
|
|
|
|
|
2021-06-05 00:07:40 +09:00
|
|
|
`lastLetter` tendrá una cadena con valor `a`.
|
2021-03-15 14:01:16 -06:00
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
# --instructions--
|
|
|
|
|
2021-02-21 08:49:16 -07:00
|
|
|
Usa <dfn>notación de corchetes</dfn> para encontrar el último carácter en la variable `lastName`.
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-02-21 08:49:16 -07:00
|
|
|
**Sugerencia:** Intenta mirar el ejemplo de arriba si te quedas atascado.
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
`lastLetterOfLastName` debe ser la letra `e`.
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(lastLetterOfLastName === 'e');
|
|
|
|
```
|
|
|
|
|
2021-02-21 08:49:16 -07:00
|
|
|
Debes usar `.length` para obtener la última letra.
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/\.length/g).length > 0);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(v){return v;})(lastLetterOfLastName);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
2021-10-31 23:08:44 -07:00
|
|
|
const lastName = "Lovelace";
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
// Only change code below this line
|
2021-10-31 23:08:44 -07:00
|
|
|
const lastLetterOfLastName = lastName; // Change this line
|
2021-02-06 04:42:36 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
2021-10-31 23:08:44 -07:00
|
|
|
const lastName = "Lovelace";
|
|
|
|
const lastLetterOfLastName = lastName[lastName.length - 1];
|
2021-02-06 04:42:36 +00:00
|
|
|
```
|