Algorithm Scripting: Title Case a Sentence

Tell us what’s happening:
Hello dear community,
My code gives a good result apparently, but the test does not pass.

Your code so far


function titleCase(str) {
let arr = str.split(" ");
let string = "";
let lo = "";
for (let i = 0; i < arr.length; i++) {
  let lo = "";
  let upper = arr[i][0].toUpperCase();

  for (let j = 1; j < arr[i].length; j++) {
    let lower = arr[i][j].toLowerCase();
    lo = lo.concat(lower);
  }
  let word = upper.concat(lo);
  string = string.concat(word, " ");
}
console.log(string);
return string.toString();
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0.

Challenge: Title Case a Sentence

Link to the challenge:

In you code the final result contains an extra space in right. You can trim the extra whitespaces by calling trim() on the result.

Change this,

return string.toString();

to

return string.toString().trim();
1 Like

Yes, that was the hidden mistake.
Thank you very much piedcipher.

1 Like

I’m happy to help you. You’re welcome :smiley:

1 Like