Basic Algorithm Scripting - Title Case a Sentence

**
I think my code is correct and its returning the same values as mentioned in task still its not approving. **

Can anyone explain what I am doing wrong in this code

Your code so far

function titleCase(str) {
  let splittedStr = str.toLowerCase().split(" ")
  let upperStr = ''
  for (let i = 0; i < splittedStr.length; i++) {
    let newStr = splittedStr[i].replace(splittedStr[i][0], splittedStr[i][0].toUpperCase())

    upperStr += newStr + ' '
  }
  console.log(upperStr)
    return upperStr;
};

titleCase("I'm a little tea pot");

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

Change your console log to:

console.log(`[${upperStr}]`);

Still its not working but found a solution there was a empty space after the word

Here is the function

function titleCase(str) {
let splittedStr = str.toLowerCase().split(" ");
let upperStr = ‘’;
for (let i = 0; i < splittedStr.length; i++) {
let word = splittedStr[i];
let capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1);
upperStr += capitalizedWord + ’ ';
}
return upperStr.trim();
}

to check try adding some string in console after the variable

We can also use this function

function titleCase(str) {
let splittedStr = str.toLowerCase().split(" ")
let upperStr = ‘’
for (let i = 0; i < splittedStr.length; i++) {
let newStr = splittedStr[i].replace(splittedStr[i][0], splittedStr[i][0].toUpperCase())

upperStr += newStr + ' '

}
return upperStr.trim();
};

The point of my suggestion about changing the console log was not to give you the code to solve your issue but rather to make you see the issue that needed to be fixed. It looks like that worked.

1 Like

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