Basic Algorithm Scripting: Title Case a SentencePassed

Tell us what’s happening:
Hey everybody,
I am on Basic Algorithm Scripting: Title Case a Sentence.
I don’t understand why the I of little is not a lowercase.
Someone can help me with that please ?
Thanks

Your code so far


function titleCase(str) {
let strSplitted = str.split('');
let result = "";

const strSplittedLowerCase = strSplitted.map( letters => { return letters.toLowerCase()});


for(let i=0 ; i<strSplittedLowerCase.length ; i++){
//console.log(strSplitted[i].toUpperCase())
if(strSplittedLowerCase[i-1] === ' ' || strSplittedLowerCase[i] === strSplittedLowerCase[0]){
  result += strSplittedLowerCase[i].toUpperCase()
}else{
  result += strSplittedLowerCase[i]
}
}
return result;
}


console.log(titleCase("I'm a little tea pot"));
console.log(titleCase("sHoRt AnD sToUt"))

Your browser information:

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

Challenge: Title Case a Sentence

Link to the challenge:

Hi PenPen,

this sub-condition causes the trouble:

strSplittedLowerCase[i] === strSplittedLowerCase[0]

It compares each lower case character to the first character, which just happens to be an “i” in the test case “I’m a little tea pot”.

Since your first sub-condition strSplittedLowerCase[i-1] === ' ' makes sure all characters preceded by a space character get capitalized, all you need as the second part of the condition is “OR it is the first character”

Like:

if(strSplittedLowerCase[i-1] === ' ' || /* it is the first character */) {
1 Like

Hi michaelsndr,
thank you very much for your answer.
I see.
So what can I do for my second condition.
I try to change the i by another letter but it is still the same result.

Your for-loop iterates already over all letters. So when the loop is at the first character, the if statement should also be true. Another way to put would be: “when the counter-variable is at the first position” … capitalize …

1 Like