Title Case A String **HELP **

Tell us what’s happening:
The algorithms seems to work when running the function, however, i can’t pass it. Where did I go wrong?

Your code so far


function titleCase(str) {
let final = "";
let strSplit = str.split(" ");
for(let i = 0; i < strSplit.length; i++){
let strToLower = strSplit[i].toLowerCase();
   let replace = strToLower.replace(strToLower.charAt(0), strToLower.charAt(0).toUpperCase());
    final = final + " " + replace;

    // console.log(final);
}
   return final;
}


titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

Check the length of the input str and compare it to the length of the final string that you are returning to find your mistake.

Thanks for that! Didn’t realise I was adding a space at the start of the string. I added the .trim method to pass the test.

1 Like