You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:FreeCodeCamp ES6模板字面量测试未通过,求排查原因

Fixing the "includes template literal" Test Failure in freeCodeCamp

Hey there! I totally get how frustrating it is when your code outputs the right result but still fails a test—let's sort this out for you.

First off, your code does use template literals, but freeCodeCamp's test for this exercise might be looking for a more idiomatic ES6 approach that makes the template literal usage more explicit to its checker. Let's break down the issue and fix it:

The Quirk in Your Current Code

Your for loop works correctly and uses template literals, but you're escaping double quotes inside the template (\"text-warning\") which isn't necessary in template strings. More importantly, the test might be expecting you to use an array method like map() to directly generate the array of template strings, rather than pushing items into an empty array with a loop.

The Fix

Replace your for loop with the map() method, which is a cleaner ES6 way to transform arrays. Here's how your updated makeList function would look:

function makeList(arr) {
  "use strict";
  // change code below this line
  const resultDisplayArray = arr.map(item => `<li class="text-warning">${item}</li>`);
  // change code above this line
  return resultDisplayArray;
}

Why This Works

  • This code still uses template literals to build each list item string, just like your original code.
  • The map() method directly returns the array of formatted strings, which aligns better with the exercise's ES6-focused intent.
  • You don't need to escape double quotes inside the template literal—template strings allow single and double quotes without escaping, making the code cleaner and more readable.

Your original logic was correct, and the output was right, but freeCodeCamp's test checker for this exercise seems to prioritize this more concise, ES6-native approach to confirm you're using template literals as intended.

内容的提问来源于stack exchange,提问作者J. Daniels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:02:06