how to capitalize the first and last of each word in javascript? [closed]

-4

Write a function that receives a phrase by parameter return that same phrase, but with the first and last letter of each word in uppercase.

    
asked by Michell Arenales 09.12.2018 в 00:44
source

2 answers

1

Here you have it, it returns "FIRST AND LAST LETTER OF THE CLOUD IN SHIFT":

function strWordsToCapsFirstLast(frase) {

    let strin = frase.toLowerCase().split("").join("").split(" ");
    for(let i = 0 ; i < strin.length ; i++){
        let len = strin[i].length-1;
        if (len > 0){
            strin[i] = strin[i].substring(0,1).toUpperCase() +strin[i].substring(1, len) + strin[i].substr(len).toUpperCase();
        }else {
            strin[i] = strin[i].toUpperCase();
        }
    }
    return strin.join(" ");
}

let specialcaps = strWordsToCapsFirstLast("primera y última letra de cada palabra en mayúscula");
alert(specialcaps);
    
answered by 09.12.2018 в 01:42
0

function firstAndLastUpperCase (word) {return word.split (""). map (function (char, index) { if (index === 0 || index === word.split (""). length-1) return char.toUpperCase (); return char; }). join ("");}

    
answered by 09.12.2018 в 18:15