Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
This code works when I execute it in Visual Studio Code
Describe your issue in detail here.

Below is what I get when I submit it in FreeCodeCamp but it still says it’s wrong. I mean know I need to change the console.log to a return statement. I’m just using the console.log to actually see the output. It comes out perfect when I execute from VSC to a browser window. I’m confused.
// console output
I’m A Little Tea Pot
I’m A Little Tea Pot
I’m A Little Tea Pot
Short And Stout
Here Is My Handle Here Is My Spout

Your code so far

function titleCase(str) {
  let newStr = str.toLowerCase()
                  .replace(/\S/, str[0].toUpperCase())
  let arr = newStr.split(' ');
  let str2 = '';
  let str3 = '';
  let finalString = '';

  while (arr.length > 0) {
    str2 = arr.shift(arr[0])
    str3 = str2.replace(/^/, ' ')
    finalString += str3.replace(str3[1], str3[1].toUpperCase());

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/ Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

It is deceptive, but your output is slightly off, regardless of where it’s ran from. Your function embeds an extra space at the beginning of the output which is why it’s failing all tests except for the one that specifies that it must return a string.

If you take a look at the output, you’ll see it’s actually:
" I’m A Little Tea Pot". Notice the space at the beginning.

The only thing you need to do, though, is to trim the final return content to get rid of any leading or trailing whitespace. And you’re set!

return finalString.trim();
1 Like

Oh my gosh Thank you! I was getting so pissed! Duh!

Man thanks. I trimmed it right up and got the wonderful “bllliiiing” from FCC upon submission!

1 Like

I think if you are asking how to output the value in the window of the browser you need to add an html tag with a attribute id with any value, as for me I used demo and this demo is the one that we will call from javascript and then assign it with the function argument value as show below

You are very welcome, Matt! Happy Coding!

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