Title Case a Sentence - Seems to work, but doesn't pass

Tell us what’s happening:
Hello, when I tested my code manually, it made the first letter of all words uppercase and all others lower case. But the tests don’t pass. Do you have any idea why? Thanks.

Your code so far


function titleCase(str) {
  //split str into arr
  let wordlist = str.split(" ");
  let answer = "";

  //iterate over array
  for (let word in wordlist) {

    //all letters lowercase
    wordlist[word] = wordlist[word].toLowerCase();

    //first letter uppercase
    answer += wordlist[word].charAt(0).toUpperCase();
    
    //add all other letters
    answer += wordlist[word].substring(1, wordlist[word].length);
    
    //space character
    answer += " ";
  }
  console.log(answer);
  return answer;
}

titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:69.0) Gecko/20100101 Firefox/69.0.

Link to the challenge:

The problem seems to be extra whitespace. Try this to get rid of any leading or trailing whitespace:

return answer.trim();
1 Like