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

1.7 KiB

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

Description

为了获得字符串的最后一个字母,您可以从字符串的长度中减去一个字母。例如,如果var firstName = "Charles" ,则可以使用firstName[firstName.length - 1]获取字符串最后一个字母的值。

Instructions

使用括号表示法查找lastName变量中的最后一个字符。 暗示
如果卡住了,请尝试查看lastLetterOfFirstName变量声明。

Tests

tests:
  - text: <code>lastLetterOfLastName</code>应为“e”。
    testString: 'assert(lastLetterOfLastName === "e", "<code>lastLetterOfLastName</code> should be "e".");'
  - text: 你必须使用<code>.length</code>来获取最后一个字母。
    testString: 'assert(code.match(/\.length/g).length === 2, "You have to use <code>.length</code> to get the last letter.");'

Challenge Seed

// Example
var firstName = "Ada";
var lastLetterOfFirstName = firstName[firstName.length - 1];

// Setup
var lastName = "Lovelace";

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

After Test

console.info('after the test');

Solution

// solution required