4 回答
TA贡献1877条经验 获得超1个赞
因此,我将以两种方式提及这些问题:如何修复代码,我将建议一种干净的方法来解决问题。
该问题与代码的正确性无关,但事实上,您更改了正在检查的变量的名称。您应该将变量“failuresList”保留在全局范围内,以作为保存结果数组的变量。但相反,您将其更改为“resultDisplayArray”。复制粘贴您的代码并仅更改名称,导致所有测试都通过” (我指的是代码中最后一行之前的行)
您不应该使用内部函数“resultDisplayArray”来包装数组创建。这就是“makeList”的用途,内部函数是多余的,因此不应该存在。
我本来就是这样做的:
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(curr => `<li class="text-warning">${curr}</li>`);
// Only change code above this line
return failureItems;
}
const failuresList = makeList(result.failure);
console.log(failuresList);
TA贡献1887条经验 获得超5个赞
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 = [];
for (let i = 0; i < result.failure.length; i++){
failureItems.push(`<li class="text-warning">${result.failure[i]}</li>`);
}
// Only change code above this line
return failureItems;
}
const failuresList = makeList(result.failure);
TA贡献1752条经验 获得超4个赞
我刚刚开始了漫长的 JavaScript 学习之旅,但据我了解,我需要运用我目前所掌握的知识。
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 = [];
for ( let i = 0; i < arr.length; i++) {
failureItems.push(`<li class="text-warning">${arr[i]}</li>`)
}
// Only change code above this line
return failureItems;
}
const failuresList = makeList(result.failure);
console.log(makeList(result.failure))
TA贡献1942条经验 获得超3个赞
您可以尝试下面这个简单的解决方案:
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 = [];
for (i=0; i < result.failure.length; i++) {
failureItems[i] = `<li class="text-warning">${result.failure[i]}</li>`;
}
// Only change code above this line
return failureItems;
}
const failuresList = makeList(result.failure);
console.log(failuresList);
添加回答
举报