freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.chinese.md

1.7 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
bd7123c9c452eddfaeb5bdef Use Bracket Notation to Find the Nth-to-Last Character in a String 1 使用括号表示法查找字符串中的第N个到最后一个字符

Description

您可以使用我们刚刚用于检索字符串中最后一个字符的相同原理来检索N到最后一个字符。例如您可以使用firstName[firstName.length - 3]获取var firstName = "Charles"字符串的倒数第三个字母的值

Instructions

使用括号表示法查找lastName字符串中倒数第二个字符。 暗示
如果卡住,请尝试查看thirdToLastLetterOfFirstName变量声明。

Tests

tests:
  - text: <code>secondToLastLetterOfLastName</code>应为“c”。
    testString: 'assert(secondToLastLetterOfLastName === "c", "<code>secondToLastLetterOfLastName</code> should be "c".");'
  - text: 你必须使用<code>.length</code>来获得倒数第二个字母。
    testString: 'assert(code.match(/\.length/g).length === 2, "You have to use <code>.length</code> to get the second last letter.");'

Challenge Seed

// Example
var firstName = "Ada";
var thirdToLastLetterOfFirstName = firstName[firstName.length - 3];

// Setup
var lastName = "Lovelace";

// Only change code below this line
var secondToLastLetterOfLastName = lastName;

After Test

console.info('after the test');

Solution

// solution required