SOLVED Basic Algorithm Scripting: Title Case a Sentence2

i tried my code and if i run it in StudioVisioCode it return me the correct answer but it doesn’t not work as answer in the excercise.
can you help me undestrand why?

function titleCase(str) {
  let arr = str.split(" ");
  let res, res2 , res3, res4 = ""
  for (let i = 0; arr.length>i; i++){
    res = arr[i][0].toUpperCase();
    res2 = arr[i].slice(1);
    res3 = res + res2;
    res4 += res3 + " "
  } 
  res4 = res4.slice(0, res4.length-1);
  console.log(res4);
  return res4;
}

this is my console

just an hunch, but try instead
console.log("'" + res4 + "'")
I can’t check myself, but see what it prints

I did a very similar algorithm in that challenge and also had the same issue. What is happening is that on line 8 you are basically telling the code to add a final white space after your last word, which means that you have one character extra than the string expected.

There are many ways to deal with that last white space that you can find easily.

A look at the challenge help reveals that this solution is far from being the most efficient or the prettiest, but most of the answers to that challenge from FCC are actually quite difficult to get to because a lot of methods we didn’t learn at this stage are involved.

@InTheMood
as you can see in line 10 i cut the last char of the string but it didn’t work.
i know my solution is far away from the best one but i studyed just one week.
give me time :slight_smile:
@ilenia
i don’t understand your suggest : console.log is jus to check before return.

what tests are failing?

what appear in the fcc console when you run the tests?

Oh! Damn sorry I read too fast into it. Your code won’t work when the string passed to the function has uppercase letters that aren’t in the first position of each word.

The function as it is only capitalize the first letter of each word. But FCC is testing with strings such as “HERE IS MY HANDLE HERE IS MY SPOUT” which should return the sentence with only the first letter of each word capitalized!

So, you probably need to ensure the string you pass to the function is transformed to lowercase from the start before you apply your capitalization.
In the FCC’s editor, you’ll see all the tests required to pass with the argument it is passing through your function and the result to be expected. If you use another editor to code, it’s a good idea to call for each of these tests so that you can compare your own console with the results expected by FCC

By the way, I was not pointing at the fact that your solution is not good enough, I was on the contrary saying that I had done the same and was comparing it to FCC’s answers which for that particular challenge were mostly using stuff that you would have not studied yet at this stage, which makes them hard to stumble upon on your own. I was in the exact same situation :wink:
These are still worth a look once you’ve implemented a solution of your own though!

1 Like

@InTheMood
STUPID ME! i did not see it return wrong just the last sentence that it’s all uppercase!
Thank you a lot for your help and your time!

1 Like