Title Case a Sentence bug

my code works in editor but doesn’t pass tests,it is not a pretty code but i think it is correct,can someone explain what’s wrong with it ?

function titleCase(str) {

    var array = str.toLowerCase();

    array = array.split(" ");

    var charachter;

    var replaced;

    var stringed = "";

      for(var i = 0; i < array.length; i++){

        charachter = array[i].charAt(0).toUpperCase();

        replaced = array[i].replace(array[i].charAt(0), charachter);

        stringed += replaced + " ";

      }

      console.log(stringed);

      return stringed;

  }

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

Hi @RUBEUSHAGRID13 ,

I think the stringed variable which you are returning has space at the end. Try trimming the space at the end before returning it.

2 Likes

thank you very much, already changed the code , you are right, it was bigger in length because of space

function titleCase(str) {

    let array = str.toLowerCase();

    array = array.split(" ");

    var charachter;

    var replaced;

      for(var i = 0; i < array.length; i++){

        charachter = array[i].charAt(0).toUpperCase();

        replaced = array[i].replace(array[i].charAt(0), charachter);

        array.splice(i,1,replaced);

      }

     var newstr = array.join(" ");

     console.log(newstr);

     return newstr;

  }

  

  titleCase("HERE IS");

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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


for next time please

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

thank you, will take it into consideration next time

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