While working on some JS challenges, I observed that when using arrow functions, the result is as expected. However, when I try the same code with a regular function, it doesn't work. Can someone please explain the difference or point out if there is a typo?
Here is the first solution (which works):
function titleCase(str) {
str = str.split(' ').map(i => i[0].toUpperCase() + i.substr(1).toLowerCase()).join(' ')
return str;
}
console.log(titleCase("I'm a liTTle tea pot")); // I'm A Little Tea Pot
And here's the second solution using a normal function (but it returns an empty string):
function titleCase2(str) {
str = str.split(' ').map(function(i, index){ i[0].toUpperCase() + i.substr(1).toLowerCase()}).join(' ')
return str;
}
console.log(titleCase2("I'm a liTTle tea pot")); // empty string