Hey everyone!
Why can’t I pass the 2nd case? I’m getting exactly the same result I’m supposed to. Any ideas?
- titleCase(“I’m a little tea pot”) should return “I’m A Little Tea Pot”.
function titleCase(str) {
var splitStr = str.toLowerCase().split(" ");
var newStr = "";
for (var i=0; i < splitStr.length; i++) {
if (i > 0) {
newStr += " ";
}
for (var j=0; j < splitStr[i].length; j++) {
if (splitStr[i][j] == splitStr[i][0]) {
newStr += splitStr[i][0].toUpperCase();
} else {
newStr += splitStr[i][j];
}
}
}
//newStr.trim();
return newStr;
}
Your code returns: I'm A LittLe Tea Pot
, note the extra capital L in LittLe.
You are currently checking if any of the letters in a word match with the first letter, and then capitalize that letter. You should only capitalize the first one. The easiest way to do this with your code: if(j === 0)
(is this the first letter) instead of if (splitStr[i][j] == splitStr[i][0])
(is this the same letter as the first letter).
1 Like
Facepalm. I checked that code SO MANY TIMES, and I couldn’t figure what I had done wrong. I really didn’t notice the extra capital “L”.
If only I had known…
Thank you so much for your help. I do appreciate it. You’ve saved my life, you’re a hero!
1 Like
This splitStr[0][0] === splitStr[0][j] would work but j === 0 is simple