DEV Community

loading...

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
viktordimitrievski profile image
Viktor

First & Fast & Last try with JS

capitalizeFirstLast = sentence => {
    var arrWords = sentence.toLocaleLowerCase().split(" ");
    var newArrWords = [];

    arrWords.forEach((x,i)=> {
        var wordLength = x.length;
        if(wordLength > 2){
            newArrWords.push(x.charAt(0).toUpperCase() + x.substr(1,wordLength-2) + x.charAt(wordLength - 1).toUpperCase());
        }
        else{
            newArrWords.push(x.toUpperCase());
        }
    });
return newArrWords.join(" ");
}