* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
67 lines
1.6 KiB
Markdown
67 lines
1.6 KiB
Markdown
---
|
||
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--
|
||
|
||
方括号表示法是一种在字符串中的特定`index`(索引)处获取字符的方法。
|
||
|
||
大多数现代编程语言,如JavaScript,不同于人类从 1 开始计数。它们是从 0 开始计数,这被称为 <dfn>基于零</dfn> 的索引。
|
||
|
||
例如, 在单词 "Charles" 中索引 0 上的字符为 "C",所以在`var firstName = "Charles"`中,你可以使用`firstName[0]`来获得第一个位置上的字符。
|
||
|
||
# --instructions--
|
||
|
||
使用方括号获取变量`lastName`中的第一个字符,并赋给变量`firstLetterOfLastName`。
|
||
|
||
**提示**
|
||
如果你遇到困难了,不妨看看变量`firstLetterOfFirstName`是如何赋值的。
|
||
|
||
# --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];
|
||
```
|