Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
why is this method not working perfectly?

Your code so far

const titleCase = function(sentence){
  
    let str = sentence.split(" ")
    for (let i = 0; i < str.length; i++){
      
      str[i]= str[i][0].toUpperCase() + str[i].substring(1);
    }
   return str.join(' ')

}

titleCase("tolu goes to school");

Your browser information:

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

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

Have you tried running the failing test to see what happens?

const titleCase = function(sentence){
  let str = sentence.split(" "); // Does this need let?
  for (let i = 0; i < str.length; i++) {
    str[i] = str[i][0].toUpperCase() + str[i].substring(1);
  }
  return str.join(' ');
}

console.log(titleCase("sHoRt AnD sToUt"));
2 Likes

Yes, I have seen the error, it requires I first of all make the sentence lowercase. Then proceed to make the first word of each sentence uppercase

1 Like

I have gotten the answer , all I needed to was to add a lowercase() to the substring(),

const titleCase = function(sentence){
  
    let str = sentence.split(" ")
    for (let i = 0; i < str.length; i++){
      str[i].toLowerCase()
      str[i]= str[i][0].toUpperCase() + str[i].substring(1).toLowerCase();
    }
   return str.join(' ')

}

titleCase("tolu goes to school");

@JeremyLT , Why do you think the let is not needed?

Generally, you should use const wherever possible

1 Like

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