Algorithm challenge problem

Algorithm challenge problem
0

#1

i am currently undertaking algorithm challenge after completing Js Course on freecode. i ran in to a ploblem when trying to solve the title case challenge. the function i implemented works for all sentences and even gives accurate result for failed case. hw can i circumvent this to move to my next challenge. this is my code

function titleCase(str) {
str=str.toLowerCase().replace(/\b\w/gi,function(val){
return val.toUpperCase();
}
);
return str;
}


#2

\w counts ’ as a word boundary since it’s a non word character between two word characters. So…

titleCase("I'm a little tea pot");
"I'M A Little Tea Pot" // output

instead of “I’m A Little Tea Pot” as expected.

http://www.regular-expressions.info/wordboundaries.html


#3

tnk u. i did not take note of the apostroph. i already solved the problem using a different approach. see my implementation below
function titleCase(str)
{
str = str.toLowerCase();
var strArr = str.split(" ");
var newstrArr = strArr.map(function (val) {
return val.replace(val.charAt(0),val.charAt(0).toUpperCase());

})
return newstrArr.join(" ");

}