Title Case A Sentence - solution not accepted

Hello,

I’m not sure if I’m missing something, but I believe I have a working solution for the Title Case A Sentence JavaScript challenge that isn’t being accepted by the editor.

  **Your code so far**

function titleCase(str) {
let words = str.split(" ");
let sentence = "";

for (let i = 0; i < words.length; i++) {
  let lower = words[i].toLowerCase();
  let upper = lower[0].toUpperCase();

  let eachWord = lower.replace(lower[0], upper);
  sentence = sentence + eachWord + " ";
  }  
  sentence.trim();
  console.log(sentence);
  return sentence;
}

titleCase("I'm a little tea pot");
titleCase("sHoRt AnD sToUt")
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/100.0.4896.127 Safari/537.36

Challenge: Title Case a Sentence

Link to the challenge:

Check the length of the string.

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

You can also do a console.log using JSON.stringify()

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

If you need more help I’d suggest you read the MDN docs on trim().

Pay close attention to the information given about the method being mutating or non-mutating and its return value.

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.

1 Like

Thank you so much, that was extremely helpful and also thanks for giving advice that will help in the future too around whether a method was mutating or non-mutating. I have never actually considered that different methods were mutating or not-mutating but it seems obvious now!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.