I have the following question:
Write a titleize (text) function that converts each first letter of each word to uppercase.
ex: titleize ("this IS just A tExT"); // correct output - > (This Is Just A Text.)
I was able to leave the first few letters of each word in capitals, but I have no idea how to change the rest of the letters to lowercase .
I found my solution verbose if they had something more elegant.
Follow the code so you can see what I'm doing:
function titleize(text) {
// Convertendo primeira letra em maiuscula.
text = text.charAt(0).toUpperCase() + text.slice(1);
for (var i = 0; i < text.length; i++) {
if (text.charAt(i) ===" ") {
// Convertendo letra após o ESPAÇO em maiuscula
var charToUper = text.charAt(i+1).toUpperCase();
// Colocando texto de antes do ESPAÇO na variável
var sliceBegin = text.slice(0, (i+1));
// colocando o texto de depois do ESPAÇO na variável
var sliceEnd = text.slice(i + 2);
// Juntando tudo
text = sliceBegin + charToUper + sliceEnd;
} else {
// NAO CONSIGO PENSAR EM COMO TRANSFORMAR O RESTANTE DAS LETRAS EM MINUSCULA
}
}
return text;
}
console.log (titleize("this IS just A tExT"));
Notice that I did nothing about the central letters of each word, so they return both uppercase and lowercase letters: /
My current output on the console:
How could I solve this problem?