Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
Describe your issue in detail here.
Happy to solve the problem after using all my learning from FreeCodeCamp, I know the answer is not perfect, since its using lots of variable and memory space, I would like to know if anyone can improve this code and may i know why its not passing the test?

Your code so far

function titleCase(str) {
  let arr = str.match(/(\w'\w+)|(\w+)/gi);
  let strr = '';
  let newStr = '';
  for(let i=0; i<arr.length; i++){
    let ii = '';
    let jj= '';
    for(let j=1; j<arr[i].length; j++){
        jj = jj + arr[i][j].toLowerCase();    
    ii = arr[i][0].toUpperCase();
    strr = ii+jj;   
    newStr = newStr + ' '+ strr;  
  return newStr

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

 I'm A Little Tea Pot
I'm A Little Tea Pot

Remove trailing space then you are good :rofl:

Thanks @WongYC-66 , you are only one going through my problem.

I resolve the issue with trim method. And the issue is due to following code.

let newStr = ‘’;
newStr = newStr + ’ '+ strr;

can you tell me like how can I better my code and not required to use trim method on string.

Thanks again

 if(i == 0){ newStr += strr; continue }

Without changing the algorithm structure, what i can think of is add an if-else to check if it is the first letter. But i would prefer the trim method, the beauty of javascript :grin:

If you want to avoid trim, you can use a split and join approach.

1 Like

can you tell me like how can I better my code and not required to use trim method on string.

not apruve - not working

Do you mind sharing your source code?

Here is my approach using split, slice and join (avoids trim):

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Hi Jeremy, to solve this problem, I have not reached split and join course. Trying to solve whatever I know. trim() method I google it since its not there in FCC.
Thanks for your suggestion.

If you want to only use things you have learned in fCC thus far, you can do almost everything with if-else and for.

Though, you don’t need to restrict yourself so severely.

1 Like

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