freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-an.../basic-javascript/use-bracket-notation-to-fin...

75 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
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];
```