My Code on "Title Case A Sentence" works, but the system flags an error

Ok… my code below works. i have passed various sample data, and it all returned correct but when i try to submit, i get a notification error, of which the value in the error still works… Please help, i just want to understand the cause.

  **Your code so far**

function titleCase(str) {
var word=str.split(" ");
var join="";
for(var i =0; i < word.length; i++)
  join += word[i].charAt(0).toUpperCase() + word[i].toLowerCase().slice(1)+" ";
  return join;


console.log(titleCase("sHoRt AnD sToUt"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0

Challenge: Title Case a Sentence

Link to the challenge:

Hi @orgus,
This is a puzzling problem. Whenever , you log the output side-by-side and look for differences there aren’t any visible.

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"))
console.log("Here Is My Handle Here Is My Spout")

However, reading your code I see that you are appending a blank space to the string. This space is being compared to the string under test so that you have one string without any trailing white space being compared to your string which has trailing white space. You need to trim the white space from the result that you return from the function.


True, i wounder why as i couldn’t find any myself. Hence me wanting to understand why it doesn’t work and what are the best practices for future occurences

damn i literally just realized it and you beat me to it by 30 seconds lmaoo


Some problems are difficult to debug. But growth as a programmer involves a lot of debugging and head scratching. One thing to learn is when to reach out for help, there’s no point in spending hours on something you can’t can’t figure out on your own when you have easy access to helpful people :slight_smile: . As a general rule I try to ask for help after about 15min of not figuring something out on my own.

1 Like

Awesome, added trim to the return value like this: return join.trim()
and it went through… Now i understand why, thank you very much

1 Like

Cool. will need to borrow and adopt the 15minute method/rule from you. I’ve spent hours on the “Why” untill now. Funny how the reason/solutions takes few seconds

Lets imagine you were not allowed to read the code. What are some of your options?

  1. Assert the function return against the expected value. Which is what the tests are doing.
console.log("I'm A Little Tea Pot" === titleCase("I'm a little tea pot")) // false
  1. You don’t see the difference so as a sanity check let’s look at the length.
console.log("I'm a little tea pot".length) // 20
console.log(titleCase("I'm a little tea pot").length) // 21
  1. Use JSON.stringify on the return (it can be very handy for logging).
console.log(JSON.stringify(titleCase("I'm a little tea pot"))) // "I'm A Little Tea Pot "

So even without reading the code, you can sometimes find the problem. Not where it is in the code, or how to fix it, just what the problem is. Which can help you narrow down the scope.


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