function titleCase(str) {
let arr = str.split(' ');
let narr= [];
for(let i in arr){
//console.log(arr[i].charAt(0).toUpperCase());
//console.log(arr[i].slice(1).toLowerCase());
narr[i] = arr[i].charAt(0).toUpperCase()+arr[i].slice(1).toLowerCase();
//console.log(arr[i].charAt(0).toUpperCase()+arr[i].slice(1).toLowerCase());
//console.log(narr[i]);
}
return narr.join('');
}
titleCase("I'm a little tea pot");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.
Instead of using for, you can use filter(),
Also inside the filter callback: arr.filter(value => { // callback }) you can:
create a variable and assign the value with lowercase method: cont lowerString = value.toLowerCase()
Then you could create another variable and substract the first characther of the string and transform with upperCase method: lowerString.charAt(0).toUpperCase(). As you can see you have the first characther with upperCase.
And finally you can return this two variables with + but keep in mind that you have to slice the first variables and remove the first character and assign the new value. return varUpperCase + varLowerString.slice(1)