Hi guys,
After hitting the basic algorithms, I’ve been getting pretty bogged down and am having to stare at each one for quite a long time. I think i’ve possibly raced through the basic JS and OOJS sections too quickly to take it all in and understand it properly, as this stuff doesn’t seem intuitive to me yet, which is frustrating.
Anyway - I’m trying to output a whole sentence with the first word capitalised… I’ve managed to get the first word in the sentence output with a capital, but I can’t get my head around the code that’s needed to loop through and add the next word to the variable so that you get the full sentence… Can anyone give me a pointer?
Also I realise my code can probably be condensed a lot…
Thanks!!
function titleCase(str) {
var lcase = str.toLowerCase();
var splstr = lcase.split(' ');
// cap each letter and extract
for (var i=0; i < splstr.length; i++) {
var capChar = splstr[i].charAt(0).toUpperCase();
var subarr = splstr[i].split('');
// now replace the first index of each subarr with capChar
var finalsub = subarr.splice(0,1,capChar);
// now join the subarr back together
var subjoin = subarr.join('');
return subjoin;
}
}