Title Case a Sentence Can't get pass

I test my code in VSCode using code runner
I think i didn’t do anything wrong

someone can tell where the error at?

code write all by meself


function titleCase(str) {
    return str.split(' ').map(function (elem) {
      var tempStr = elem[0].toUpperCase();
      for (var i = 1, j = elem.length; i < j; i += 1) {
          tempStr += elem[i].toLowerCase();
      }
      return tempStr;
  }).reduce(function (sum, elem) {
      return sum + elem + ' ';
  }, '');
}

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

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a 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.

markdown_Forums

thanks a lot !
first time post not realize i can use markdown here

Hey @RocMark can you please provide a sample where your code is failing? It’ll be easier to debug then!

I didn’t get any error message in vscode using code runner or just run code in freecodecamp

I’m trying to use these code to get pass the practice freecodecamp offer at
Front End Development Certification / Basic Algorithm Scripting / Title Case a Sentence

I have try all string freeCodeCamp provide
they all show the correct answer
I don’t know what i missing

By the way if i got something wrong writing code with freeCodeCamp will error message appear in chrome console?

yes friend you can do a console.log to show whatever test cases might be producing error for you! I’ll meanwhile try to run your code for the said challenge and see what might be going wrong :slight_smile:

1 Like

hey @RocMark I tested your code and found out that your answer is always having an extra tailing space. For example, instead of just writing out “Hello World” it would produce "Hello World ". (notice this extra space before the ending double inverted comma).

Please find a way to remove this extra space before returning the result string and you’ll be good to go! :smiley:

1 Like

thanks a lot !
no wonder it can’t get pass

Glad you fixed it!

A couple of notes about your actual solution, the map and reduce parts are a little strange to me.

The function you map with has a for loop that I think you would do better to get rid of - str.toLowerCase actually works on a whole string, not just on a single character, so you can use this on the whole substring in one go!

The reduce adds all the words together again - there’s no need for this however, as there is an inverse operation to split(' '), it’s called join(' ')

1 Like

here is my new update code

would you mind to share your code?
thanks for the advice it helps a lot.

I use elem[0] to target the first character toUpperCase and Restructuring the string

function titleCase(str) {
      return str.split(' ').map(function(elem) {
          return elem[0].toUpperCase() + elem.toLowerCase().substr(1, elem.length - 1);
      }).join(' ');
}

Please excuse my poor English.

Sorry for slow reply, and don’t worry about your english - if we don’t understand something you’ve said we can always ask!

My solution to the problem actually looks pretty much like yours, though I used slice instead of substr, and I took the slice before using toLowerCase

Nice work!