What's wrong with my code guys? The outputs are matching exactly but still the tests fail

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

function titleCase(str) {
let reg = /(\S+)/g;
let a=str.match(reg);
let b=[];
let s="";
for(let i=0;i<a.length;i++){
    for(let j=0;j<a[i].length;j++){
    s+=" ";

return s;

titleCase("I'm a little tea pot");
console.log("I'm A Little Tea Pot");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.154

Challenge: Title Case a Sentence

Link to the challenge:

Changing console.log call to something like console.log('"' + s + '"') should give you an idea what’s happening.

I get it thank you. Will try to fix it.

What do you think about my code? Is it too messy? Is it alright? How could I have done it better? Scratching my head to fix the last space

Some parts are a bit more complicated than they could be. For example splitting sentence with regex, while it could be achieved with simple string method. Or b[i][0]=b[i][0].toUpperCase();, which is being run multiple times while iterating over letters in the same word.

Readability would be also improved with using better variable names, rather than single letters.

Having all that said, first step is really making it work, after that there’s time to look closer where code can be improved or simplified.

1 Like

If I take the toUppercase outside the loop than only the lowercase values will be concatenated to the string resulting in just lowercase strings.

Solved the ending space with this:

let check=/\s$/;
return s.replace(check,'');

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