I try to make this excercise:
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
My code works, but I don’t know why adding i++ in first if fixing it. How does this code work?
My code:
function titleCase(str) {
const toLowerC = str.toLowerCase();
let word = ""
for (let i = 0; i < toLowerC.length; i++) {
if (i === 0) {
word += toLowerC[i].toUpperCase();
i++ **// Why does adding i++ make the code work? I was thinking that this code should work like this: else is triggered when the first and second if are false..."
**
}
if (toLowerC[i] === " ") {
word += toLowerC[i] + toLowerC[i + 1].toUpperCase();
i++
} else {
word += toLowerC[i]
}
}
return word;
}
titleCase("I'm a little tea pot");
I’ve tried OpenAI, but it gets confused, or maybe I’m phrasing my questions incorrectly.
I’ve seen many other ways to complete this excercise thats not my point to make my code better.
2
Answers
The algorithm is easier to understand if we introduce a boolean variable
beginningOfWord
, which is truefor
loop and(Note if the string contains two consecutive spaces, the algorithm tries to uppercase the second, but this simply has no effect.)
The
i++
is necessary in your case because you have two separate if-statements following one another.The first if-statement handles the case for when you’re on the first letter. When that runs, you’ve handled the first letter (ie, index 0), however, since the next
if
statement that follows it isn’t anelse if
, but rather its ownif
, you’ll now be updatingword
again with the current letter, regardless of if it goes into theif
orelse
branch of the second if-statement, as in both cases you’re performingword += toLowerC[i]
. That’s why if you performi++
in your first if-statement, the second if-statement that runs will be checking the next letter, and so you won’t be trying to handle the first one twice as you would be without thei++
.It might be easier to change your second
if
to anelse if
, as well as change the condition to check if the previous character was a space to determine if the current character should be made uppercase, this way, you’re only handling one character at a time:I know you mentioned that you’re not after alternatives, but it might help future readers or give you some more insights. I would most likely use a regular expression to match the characters that need uppercasing and then use
.replace()
to update those. This matches each character followed by a space or at the beginning of the string and uppercases those: