I want to convert the given string to a title case:
const titleCase = function (text) {
if (text === "") return "";
let textArr = text.split(" ");
const outputArr = textArr.map(
ele => ele.toLowerCase().replace(ele[0], ele[0].toUpperCase())
);
const output = outputArr.join(" ");
return output;
};
const test1 = titleCase("this is an example");
const test2 = titleCase("WHAT HAPPENS HERE");
console.log(test1);
console.log(test2);
test1
gives me the right result This Is An Example
but test2 returns what happens here
which is not the result that I want.
I am confused... where did it go wrong?