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

freeCodeCamp Title Case a Sentence算法挑战问题求助

Troubleshooting Your freeCodeCamp Title Case Algorithm Issue

Hey there! I totally get the frustration when your code looks like it's working but won't pass the platform checks—let's break down the most common pitfalls with this challenge and get you unstuck.

First, let's recap the challenge requirements clearly to make sure we're aligned:

Return the provided string with the first letter of each word capitalized and the rest of the letters lowercase. All words (including short ones like "the" or "and") need their first letter capitalized.

Common Reasons Your Code Might Fail

Let's go through the usual suspects that trip people up:

  • You're not normalizing the entire string to lowercase first
    If your code only capitalizes the first letter of each word but leaves existing uppercase letters in the rest of the word intact, it'll fail test cases with mixed casing. For example, input "hELLO wORLD" should output "Hello World", not "HELLO WORLD" or "Hello wORLD".

  • You're not handling edge cases with spaces
    freeCodeCamp's test suite includes cases with leading/trailing spaces or multiple consecutive spaces. If your code splits on single spaces (split(" ")) without cleaning up empty string entries, you might end up with extra spaces in your output. For example, input " hello world " should become "Hello World", not " Hello World ".

  • Your logic breaks with single-character words
    Test cases like "i am a test" need to become "I Am A Test". If your code assumes words have at least two characters (e.g., trying to slice from index 1 without checking length), it might produce unexpected results for single-letter words.

A Robust Solution to Compare Against

Here's a solution that handles all these edge cases:

function titleCase(str) {
  // Convert the entire string to lowercase first to normalize casing
  const lowerStr = str.toLowerCase();
  // Split on any number of whitespace characters (handles multiple spaces)
  const words = lowerStr.split(/\s+/);
  // Capitalize the first letter of each word
  const titleCasedWords = words.map(word => {
    return word.charAt(0).toUpperCase() + word.slice(1);
  });
  // Join back with single spaces and trim leftover edge spaces
  return titleCasedWords.join(' ').trim();
}

How to Debug Your Code

  1. Test your code against these specific cases to see where it fails:
    • Input: "sHoRt AnD sToUt" → Should output "Short And Stout"
    • Input: " hello world " → Should output "Hello World"
    • Input: "i" → Should output "I"
    • Input: "the quick brown fox" → Should output "The Quick Brown Fox"
  2. Double-check if you're converting the entire string to lowercase before processing individual words.
  3. Verify that your splitting logic handles multiple spaces correctly (using a regex like /\s+/ instead of " " helps here).

If you share your current code, I can help pinpoint exactly where it's going wrong—but even without it, checking these common issues should get you past the challenge!

内容的提问来源于stack exchange,提问作者Luke Orth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:02:45