Tell us what’s happening:
I’ve written this code for the challenge. Everything’s outputing correctly, but freeCodeCamp’s editor is saying it’s wrong. Please tell me why the editor is saying this.
I may also be incorrect, so please don’t go harsh on me, and explain what went wrong.
/*
function titleCase(str) {
var str1 = str.split(' ');
var regex = /\w/;
var firstchar = '';
var output = '';
for (var i = 0; str1.length > i; i++) {
var str2 = str1[i].replace(regex, ' ');
firstchar = str1[i].slice(0, 1);
firstchar = firstchar.toUpperCase();
var tempstr = firstchar + str1[i].slice(1).toLowerCase();
output += tempstr + ' ';
}
str = output;
//console.log (str);
return str;
}
*/
function titleCase(str) {
var temp = str.toLowerCase().replace(/(^|\s)\S/g, ' ');
console.log(temp)
return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());
}
titleCase("sHorT aNd stOuT")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36
.
Challenge: Title Case a Sentence
Link to the challenge:
Thanks