2021-06-15 03:34:20 +09:00
|
|
|
---
|
|
|
|
id: bd7123c9c450eddfaeb5bdef
|
|
|
|
title: Usare la notazione parentesi per trovare l'n-esimo carattere in una stringa
|
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cWPVJua'
|
|
|
|
forumTopicId: 18343
|
|
|
|
dashedName: use-bracket-notation-to-find-the-nth-character-in-a-string
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
Puoi usare <dfn>la notazione a parentesi</dfn> anche per ottenere il carattere in altre posizioni all'interno di una stringa.
|
|
|
|
|
|
|
|
Ricorda che i computer iniziano a contare da `0`, quindi il primo carattere è in realtà il carattere zero.
|
|
|
|
|
|
|
|
Esempio:
|
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const firstName = "Ada";
|
|
|
|
const secondLetterOfFirstName = firstName[1];
|
2021-06-15 03:34:20 +09:00
|
|
|
```
|
|
|
|
|
|
|
|
`secondLetterOfFirstName` dovrebbe acere un valore stringa `d`.
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
Cerchiamo di impostare `thirdLetterOfLastName` per eguagliare la terza lettera della variabile `lastName` usando la notazione parentesi.
|
|
|
|
|
|
|
|
**Suggerimento:** Prova a guardare l'esempio qui sopra se ti blocchi.
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
La variabile `thirdLetterOfLastName` dovrebbe avere il valore di `v`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(thirdLetterOfLastName === 'v');
|
|
|
|
```
|
|
|
|
|
|
|
|
Dovresti usare la notazione a parentesi.
|
|
|
|
|
|
|
|
```js
|
2022-03-23 19:52:04 +05:30
|
|
|
assert(code.match(/thirdLetterOfLastName\s*=\s*lastName\s*\[\s*\d\s*\]/));
|
2021-06-15 03:34:20 +09:00
|
|
|
```
|
|
|
|
|
|
|
|
# --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 03:34:20 +09:00
|
|
|
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
const thirdLetterOfLastName = lastName; // Change this line
|
2021-06-15 03:34:20 +09:00
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const lastName = "Lovelace";
|
|
|
|
const thirdLetterOfLastName = lastName[2];
|
2021-06-15 03:34:20 +09:00
|
|
|
```
|