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

1.7 KiB
Raw Blame History

id title challengeType videoUrl forumTopicId dashedName
bd7123c9c549eddfaeb5bdef 使用方括号查找字符串中的第一个字符 1 https://scrimba.com/c/ca8JwhW 18341 use-bracket-notation-to-find-the-first-character-in-a-string

--description--

方括号表示法(Bracket notation)是一种在字符串中的特定 index索引处获取字符的方法。

大多数现代编程语言,如 JavaScript不同于人类从 1 开始计数。 它们是从 0 开始计数。 这被称为基于零(Zero-based)的索引。

例如,单词 Charles 的索引 0 的字符是 C。 所以如果 const firstName = "Charles",你可以通过 firstName[0] 得到字符串第一个字母的值。

示例:

const firstName = "Charles";
const firstLetter = firstName[0];

firstLetter 值为字符串 C

--instructions--

使用方括号获取变量 lastName 中的第一个字符,并赋给变量 firstLetterOfLastName

提示: 如果卡住了,请尝试查看上面的示例。

--hints--

firstLetterOfLastName 变量值应该为 L

assert(firstLetterOfLastName === 'L');

应该使用方括号表示法。

assert(code.match(/firstLetterOfLastName\s*=\s*lastName\s*\[\s*\d\s*\]/));

--seed--

--after-user-code--

(function(v){return v;})(firstLetterOfLastName);

--seed-contents--

// Setup
let firstLetterOfLastName = "";
const lastName = "Lovelace";

// Only change code below this line
firstLetterOfLastName = lastName; // Change this line

--solutions--

let firstLetterOfLastName = "";
const lastName = "Lovelace";

// Only change code below this line
firstLetterOfLastName = lastName[0];