--- id: 587d7b8a367417b2b2512b4e title: Create Strings using Template Literals challengeType: 1 videoUrl: '' localeTitle: 使用模板文字创建字符串 --- ## Description
ES6的一个新功能是模板文字 。这是一种特殊类型的字符串,可以更轻松地创建复杂字符串。模板文字允许您创建多行字符串并使用字符串插值功能来创建字符串。考虑以下代码:
const person = {
name: "Zodiac Hasbro",
age: 56
};

//具有多行和字符串插值的模板文字
const greeting =`您好,我的名字是${person.name}!
我是${person.age}岁。`

的console.log(greeting); //打印
//你好,我的名字是Zodiac Hasbro!
//我今年56岁
那里发生了很多事情。首先,例如使用反引号( ` ),而不是引号( '" ),换行字符串。其次,请注意,该字符串是多线,无论是在代码和输出。这节省了插入\n串内。上面使用的${variable}语法是占位符。基本上,您不必再使用+运算符连接。要将变量添加到字符串,只需将变量放在模板字符串中并用${包装它}同样,您可以在您的字符串表达式的其他文字,例如${a + b}这个新创建的字符串的方式为您提供了更大的灵活性,以创建强大的字符串。
## Instructions
使用带有反引号的模板文字语法来显示result对象的failure数组的每个条目。每个条目都应该包含在一个带有class属性text-warningli元素中,并列在resultDisplayArray
## Tests
```yml tests: - text: resultDisplayArray是一个包含result failure消息的数组。 testString: 'assert(typeof makeList(result.failure) === "object" && resultDisplayArray.length === 3, "resultDisplayArray is a list containing result failure messages.");' - text: resultDisplayArray是所需的输出。 testString: 'assert(makeList(result.failure).every((v, i) => v === `
  • ${result.failure[i]}
  • ` || v === `
  • ${result.failure[i]}
  • `), "resultDisplayArray is the desired output.");' - text: 使用了模板字符串 testString: 'getUserInput => assert(getUserInput("index").match(/`.*`/g), "Template strings were not used");' ```
    ## Challenge Seed
    ```js const result = { success: ["max-length", "no-amd", "prefer-arrow-functions"], failure: ["no-var", "var-on-top", "linebreak"], skipped: ["id-blacklist", "no-dup-keys"] }; function makeList(arr) { "use strict"; // change code below this line const resultDisplayArray = null; // change code above this line return resultDisplayArray; } /** * makeList(result.failure) should return: * [ `
  • no-var
  • `, * `
  • var-on-top
  • `, * `
  • linebreak
  • ` ] **/ const resultDisplayArray = makeList(result.failure); ```
    ## Solution
    ```js // solution required ```