freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-character-in-a-string.md
2020-10-06 23:10:08 +05:30

1.5 KiB

id, challengeType, videoUrl, forumTopicId, localeTitle
id challengeType videoUrl forumTopicId localeTitle
bd7123c9c450eddfaeb5bdef 1 https://scrimba.com/c/cWPVJua 18343 使用方括号查找字符串中的第N个字符

Description

你也可以使用方括号来获得一个字符串中的其他位置的字符。 请记住,程序是从0开始计数,所以获取第一个字符实际上是[0]。

Instructions

让我们使用方括号,把lastName变量的第三个字符赋值给thirdLetterOfLastName提示
如果你遇到困难了,看看secondLetterOfFirstName变量是如何做的。

Tests

tests:
  - text: <code>thirdLetterOfLastName</code>的值应该是<code>v</code>。
    testString: assert(thirdLetterOfLastName === 'v');
  - text: 你应该使用方括号。
    testString: assert(code.match(/thirdLetterOfLastName\s*?=\s*?lastName\[.*?\]/));

Challenge Seed

// Example
var firstName = "Ada";
var secondLetterOfFirstName = firstName[1];

// Setup
var lastName = "Lovelace";

// Only change code below this line.
var thirdLetterOfLastName = lastName;


After Test

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

Solution

var lastName = "Lovelace";
var thirdLetterOfLastName = lastName[2];