DEV Community

Discussion on: Daily Challenge #190 - capitalizeFirstLast

Collapse
 
sabbin profile image
Sabin Pandelovitch • Edited

Another JS example

const capitalizeFirstAndLast = string => {
    const splitString = str => str.split(' ');
    const fixWords = array =>
        array.map(v =>
            v.length < 2
                ? v.toUpperCase()
                : `${v.charAt(0).toUpperCase()}${v.toLowerCase().substr(1, v.length - 2)}${v.charAt(v.length - 1).toUpperCase()}`
        );
    const joinString = array => array.join(' ');

    return string
        |> splitString
        |> fixWords
        |> joinString
};

//TESTING
const tests = [
    "and still i rise",
    "when words fail music speaks",
    "WHAT WE THINK WE BECOME",
    "hello",
];

console.log(tests.map(capitalizeFirstAndLast));
/*
[
  'AnD StilL I RisE',
  'WheN WordS FaiL MusiC SpeakS',
  'WhaT WE ThinK WE BecomE',
  'HellO'
]
*/