Help Basic Algorithm Scripting: Title Case a Sentence

function titleCase(str) {

  • str=str.toLowerCase();*
  • var mass=str.split(" ") *
  • let b="";*
  • for (var i=0;i<str.length;i++){ *
  • b=b+mass[i][0].toUpperCase()+mass[i].substring(1,)+" "; *
  •  console.log(b)*
  • }*
  • return(b);*
  • titleCase(“I’m a little tea pot”);*

Tell me why console.log(b) it displays the values, and b changing is empty


I’m A
I’m A Little
I’m A Little Tea
I’m A Little Tea Pot

b= not

It’s very hard to read your code, because it is unformatted, but are you perhaps adding a space onto the end of the string?

The question is why return (b) is empty

function titleCase(str) {
   var mass=str.split(" ") ;
   var b="";
         for (var i=0;i<str.length;i++){ 
                  b+=mass[i][0].toUpperCase()+mass[i].substring(1,)+" "; 
 titleCase("sHoRt AnD sToUt");

Spaces tried to delete .trim() but it does not work

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Well, thanks, but you can answer the question where is 123 ?? Why did the console not work ??

This code errors out because you have an index out of bounds error. As you progress through the for loop, mass[i] is undefined and therefor mass[i][0] causes an error.

Thank you understood,