Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
The code is working properly based on console.log() output, but it can’t pass through the test.

Please check whether my code is legit as the solution for this task & if it is, then you might would want to add this option of code to a solution list, so test would endorse it.

Thank you for all your efforts put into this course. It helps me a lot! You guys are true heroes! It’s really amazing experience!

Your code so far

function titleCase(str) {
  let arr = str.split(' ')
  let newstr = '';
  for(let element of arr){
    newstr += element[0].toUpperCase() + element.toLowerCase().slice(1) + ' '
  }
  console.log(newstr)
  return newstr
}

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

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

It isnt working bc the way it is written it is adding a ’ ’ (extra space) at end. This space doesnt show on console.log bc its just a space.

Consider using newstr.push(…) with your code in the parenthases (but not the + " “). Then use newstr.join(” ") to create a string from array. This will create your string without the space. Hope this helps!

1 Like

hello and welcome to fcc forum :slight_smile:

given whats been already said, maybe try using typical “for” instead of your attempted for loop with an index so that you can easily “check” that last “split” element and adjust your logic accordingly to pass this step

for(let i=0; i < arr.length; i++)

happy learning :slight_smile:

Thank you so much, sir!
Now I get it!
Much appreciation! I owe you a cookie at least :slight_smile:

1 Like

Thanks for welcoming.
I’ll try to do that.
I’m a newbie here, trying to learn new things, as I’m not a CS guy bc of my education. So any help & communication recieved is what I really value & cherish.
Your prompt response & help is priceless!
& I’m astonished by this community!

1 Like

I feel like it is a really good community. Keep up the good work

2 Likes