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.md
2020-09-29 22:09:05 +02:00

1.9 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, localeTitle
id title challengeType videoUrl forumTopicId localeTitle
bd7123c9c452eddfaeb5bdef Use Bracket Notation to Find the Nth-to-Last Character in a String 1 https://scrimba.com/c/cw4vkh9 18344 使用方括号查找字符串中的第N个字符到最后一个字符

Description

我们既可以获取字符串的最后一个字符也可以用获取字符串的倒数第N个字符。 例如,你可以这样firstName[firstName.length - 3]操作来获得var firstName = "Charles"字符串中的倒数第三个字符。

Instructions

使用方括号来获得lastName字符串中的倒数第二个字符。 提示
如果你遇到困难了,不妨看看thirdToLastLetterOfFirstName变量是如何做到的。

Tests

tests:
  - text: <code>secondToLastLetterOfLastName</code>应该是"c"。
    testString: assert(secondToLastLetterOfLastName === 'c');
  - text: 你需要使用<code>.length</code>获取倒数第二个字符。
    testString: assert(code.match(/\.length/g).length === 2);

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

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

Solution

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

var lastName = "Lovelace";
var secondToLastLetterOfLastName = lastName[lastName.length - 2];