使用模板字面的字符串(freecodecamp笔记)

80 阅读1分钟

ES6模板字面是一个新的特性,允许你创建多行字符串,并使用字符串插值功能来创建字符串。

使用带后缀的模板字面语法来创建一个列表元素(li)字符串的数组。每个列表元素的文本应该是来自结果对象上的失败属性的数组元素之一,并且有一个值为text-warning的类属性。makeList函数应该返回列表项字符串的数组。

使用一个迭代器方法(任何种类的循环)来获得所需的输出(如下所示):

[
  '<li class="text-warning">no-var</li>',
  '<li class="text-warning">var-on-top</li>',
  '<li class="text-warning">linebreak</li>'
]

由于我们被要求使用一个迭代器,我们将使用map函数,这里有一篇很好的文章供你检查map在处理数组中的用法www.digitalocean.com/community/t…

这样,我们得到的练习是

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak"],
  skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {

  const failureItems //


  return failureItems;
}

const failuresList = makeList(result.failure);

在使用map函数的情况下,答案是

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak"],
  skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
  // Only change code below this line
  const failureItems = arr.map(e=>`<li class="text-warning">${e}</li>`)

  // Only change code above this line

  return failureItems;
}

const failuresList = makeList(result.failure);