Tell us what’s happening:
Hi guys, I solved this problem thanks to a video and I understand the general procedure but I still have some questions:
1- I didn’t know I could “concatenate” string methods (inside the code:
str.toLowerCase().split(" ")
). Is this a good practice? Can I do this similar “concatenation” with array methods as well?
2- I don’t understand how exactly the +
operator works inside the for loop body. It’s joining the 0-index of each element of words
with their respective remaining characters, but how the program knows that it needs to join the characters without spaces between them?
Thanks!
Your code so far
function titleCase(str) {
var words = str.toLowerCase().split(" ")
for (var i = 0; i < words.length; i++){
words[i] = words[i][0].toUpperCase() + words[i].slice(1);
}
return words.join(" ");
}
titleCase("I'm a little tea pot")// returns: I'm A Little Tea Pot
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0
.
Challenge: Title Case a Sentence
Link to the challenge: