DEV Community

Discussion on: Daily Challenge #180 - SMS Shortener

Collapse
 
jehielmartinez profile image
Jehiel Martinez

js recursive

function shortener(str) {
    const regex = /\s+(\S*)$/g;

    if(str.length <= 160){
        return str;
    };

    let match = str.match(regex);
    if(!match){
        return 'No place to trim'
    }

    let trimmed = match[0].trim();
    let capital = trimmed.replace(/^./, trimmed[0].toUpperCase());

    return shortener(str.replace(regex, capital));
};