# Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
I have no console errors but it does not pass. I result with with correct strings, checked with console.log and when running test says they don’t match but, they do match when i console log. Any help on why this way doesn’t work would be appreciated!!

``````function titleCase(str) {
let result = ''

// splitting string as is
const splitStr = str.split(' ')

// function takes each word of the split string
for (const eachWord of splitStr) {
// x is the first letter for each word
let x = eachWord.slice(0, 1)

// y is the rest of the letters for each word
let y = eachWord.slice(1, eachWord.length)

// repairs each word with first letter uppercase rest lowercase and space between each word
result += `\${x.toUpperCase()}\${y.toLowerCase()} `

}
return result
}

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

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36`

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Take a look at what function returns, with additional character indicating where exactly in the printout starts and ends returned string: `console.log(`“\${titleCase(“I’m a little tea pot”)}”`);`

Did some more research, and im still unsure why but , when i result with an array to be joined on return code passes, if i result with a string and return the string code does not pass. In the end both outputs were the same, however only this one passed.

Updated Code With Updated Comments! This is a solution.

MySolution

This text will be hidden

``````function titleCase(str) {
// use this empty array to push each modified word from the for loop
let result = []

const splitStr = str.split(' ')

for (const eachWord of splitStr) {
// eachWord[0] === eachWord.slice(0, 1) this way was shorter
let x = eachWord[0]
// removed eachWord.length from slice ending, not needed functions the same
let y = eachWord.slice(1)
// instead of adding each item to a result string, pushing it to an array to be rejoined as a string later works, this was the bug!
result.push(`\${x.toUpperCase()}\${y.toLowerCase()}`)

}
// rejoined result as a string
return result.join(' ')
}

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

In the second case there’s always one, additional blank space at the end of returned string.

Ah i see now! when i was adding each string per loop i had that blank space for between the words without realizing it adds the end of the last word as well, which is why it didnt pass due to additional blank space, thank you!

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