Quantcast
Channel: freeCodeCamp Challenge Guide: Title Case a Sentence
Viewing all articles
Browse latest Browse all 50

freeCodeCamp Challenge Guide: Title Case a Sentence

$
0
0
                                MY SOLUTION
titleCase(Str) {
    
     var oldArr = str.toLowerCase().split(' ');
     var newArr = [];
     
     for (var i in oldArr) {
         var newWord = oldArr[i][0].toUpperCase() + oldArr[i].slice(1);
         newArr.push(newWord);
        }
     return newArr.join(' ');
}

there’s no need for:

for (var i = 0; i < oldArr.length; i++) {
}

because:

for (var i in oldArr) {
}

will loop through the entire length of oldArr without the need of a loop counter.

Read full topic


Viewing all articles
Browse latest Browse all 50