Files
freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-first-character-in-a-string.md
2021-03-14 21:20:39 -06:00

75 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: bd7123c9c549eddfaeb5bdef
title: 使用方括号查找字符串中的第一个字符
challengeType: 1
videoUrl: 'https://scrimba.com/c/ca8JwhW'
forumTopicId: 18341
dashedName: use-bracket-notation-to-find-the-first-character-in-a-string
---
# --description--
方括号表示法(<dfn>Bracket notation</dfn>)是一种在字符串中的特定 index索引处获取字符的方法。
大多数现代编程语言如JavaScript不同于人类从 1 开始计数。 它们是从 0 开始计数。 这被称为基于零(<dfn>Zero-based</dfn>)的索引。
例如,单词 `Charles` 的索引 0 的字符是 `C`。 所以在 `var firstName = "Charles"` 中,你可以使用 `firstName[0]` 来获得第一个位置上的字符。
示例:
```js
var firstName = "Charles";
var firstLetter = firstName[0];
```
`firstLetter` 值为字符串 `C`
# --instructions--
使用方括号获取变量 `lastName` 中的第一个字符,并赋给变量 `firstLetterOfLastName`
**提示:** 如果卡住了,请尝试查看上面的示例。
# --hints--
`firstLetterOfLastName` 变量值应该为 `L`
```js
assert(firstLetterOfLastName === 'L');
```
应该使用方括号表示法。
```js
assert(code.match(/firstLetterOfLastName\s*?=\s*?lastName\[.*?\]/));
```
# --seed--
## --after-user-code--
```js
(function(v){return v;})(firstLetterOfLastName);
```
## --seed-contents--
```js
// Setup
var firstLetterOfLastName = "";
var lastName = "Lovelace";
// Only change code below this line
firstLetterOfLastName = lastName; // Change this line
```
# --solutions--
```js
var firstLetterOfLastName = "";
var lastName = "Lovelace";
// Only change code below this line
firstLetterOfLastName = lastName[0];
```