Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:

I have written a code to convert first letter of a sentence to uppercase. the console log output shows the correct answer but the course says its still wrong. could you please point out what’s wrong with my code here?

Your code so far

function titleCase(str) {
  let a = str.split(" ")
  let b = ""
  for(let i=0;i<a.length;i++){
    b = b + " " +a[i].charAt(0).toUpperCase()+a[i].slice(1).toLowerCase()
  }
  console.log(b)
  return b
}

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

Your browser information:

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

Challenge Information:

Basic Algorithm Scripting - Title Case a Sentence

Hi @varshansaravanan26

Your code is placing a space at the start of each string.

" I’m A Little Tea Pot"

Happy coding

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