Hey, all. I just finished the Title Case Sentence challenge, and it almost broke my brain. After some thinking and googling this is what I came up with. How did I do? I’m sure there are shorter/simpler ways of doing this, but would this be reasonable for actual application?
function titleCase(str) {
var secondArray = [];
var newStr = str.toLowerCase();
//split the string into an array of strings
var firstArray = newStr.split(" ");
//Do all the things
for (var i = 0; i < firstArray.length; i++){
var a = firstArray[i].substr(0,1).toUpperCase();
var b = firstArray[i].substr(1);
var result = a + b;
secondArray.push(result);
}
//recombine array into one string
var finalStr = secondArray.join(" ");
//return result
return finalStr;
}
I am not sure if yours would be reasonable or not, here is what I did if this helps:
function titleCase(str) {
var words = str.split(' ');
for (var index = 0; index < words.length; index++) {
words[index] = words[index].charAt(0).toUpperCase() + words[index].slice(1).toLowerCase();
}
return words.join(' ');
}
titleCase("sHoRt AnD sToUt");
function titleCase(str) {
var arr=str.toLowerCase().split(’’);
for (var i=0;i<arr.length;i++){
if (arr[i]===" "){
arr[i+1]=arr[i+1].toUpperCase();
}
}
arr[0]=arr[0].toUpperCase();
var end=arr.join(’’);
return end;
}