2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: bd7123c9c549eddfaeb5bdef
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 使用方括号查找字符串中的第一个字符
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/ca8JwhW'
|
|
|
|
|
forumTopicId: 18341
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: use-bracket-notation-to-find-the-first-character-in-a-string
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
方括号表示法(<dfn>Bracket notation</dfn>)是一种在字符串中的特定 index(索引)处获取字符的方法。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-05-09 19:42:02 +00:00
|
|
|
|
大多数现代编程语言,如 JavaScript,不同于人类从 1 开始计数。 它们是从 0 开始计数。 这被称为基于零(<dfn>Zero-based</dfn>)的索引。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-11-06 15:56:52 +00:00
|
|
|
|
例如,单词 `Charles` 的索引 0 的字符是 `C`。 所以如果 `const firstName = "Charles"`,你可以通过 `firstName[0]` 得到字符串第一个字母的值。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
示例:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2021-11-06 15:56:52 +00:00
|
|
|
|
const firstName = "Charles";
|
|
|
|
|
const firstLetter = firstName[0];
|
2021-02-06 04:42:36 +00:00
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`firstLetter` 值为字符串 `C` 。
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
使用方括号获取变量 `lastName` 中的第一个字符,并赋给变量 `firstLetterOfLastName`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
**提示:** 如果卡住了,请尝试查看上面的示例。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`firstLetterOfLastName` 变量值应该为 `L` 。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(firstLetterOfLastName === 'L');
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
应该使用方括号表示法。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```js
|
2022-03-23 14:22:04 +00:00
|
|
|
|
assert(code.match(/firstLetterOfLastName\s*=\s*lastName\s*\[\s*\d\s*\]/));
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(function(v){return v;})(firstLetterOfLastName);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
2021-11-06 15:56:52 +00:00
|
|
|
|
let firstLetterOfLastName = "";
|
|
|
|
|
const lastName = "Lovelace";
|
2021-01-13 02:31:00 +00:00
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
firstLetterOfLastName = lastName; // Change this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
2021-11-06 15:56:52 +00:00
|
|
|
|
let firstLetterOfLastName = "";
|
|
|
|
|
const lastName = "Lovelace";
|
2021-01-13 02:31:00 +00:00
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
firstLetterOfLastName = lastName[0];
|
|
|
|
|
```
|