xxxxxxxxxx
function capitalize1(str) {
const arrOfWords = str.split(" ");
const arrOfWordsCased = [];
for (let i = 0; i < arrOfWords.length; i++) {
const word = arrOfWords[i];
arrOfWordsCased.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
}
return arrOfWordsCased.join(" ");
}
xxxxxxxxxx
//leetcode capitalize problem
function capitalize(str) {
let arrOfWords = str.split(" ");
let newStr= [];
for(let i=0 ; i<arrOfWords.length ; i++){
const word = arrOfWords[i]
if(word.length>2){
newStr.push(word[0].toUpperCase()+word.slice(1).toLowerCase())
}else{
newStr.push(word.toLowerCase())
}
}
return newStr.join(" ")
}
xxxxxxxxxx
function capitalize(str) {
let x = str.split(" ").map((m) => m.split(""));
console.log(x);
for (let i = 0; i < x.length; i++) {
for (let y = 0; y < x[i].length; y++) {
if( x[i].length > 2){
x[i][0] = x[i][0].toUpperCase();
if (y !== 0) {
x[i][y] = x[i][y].toLowerCase();
}
}else{
x[i][y] = x[i][y].toLowerCase();
}
}
}
return x
.map((n) => {
return n.join("");
})
.join(" ");
}