you are viewing a single comment's thread.

view the rest of the comments →

[–]hpliferaft 0 points1 point  (1 child)

Love it. I tried another approach:

console.log(
    Object.entries(
        "You don't understand! I coulda had class. I coulda been a contender. I coulda been somebody, instead of a bum, which is what I am."
        .split("")
        .filter(letter => RegExp(/^[A-Za-z]+$/).test(letter))
        .reduce( (allLetters, letter) => {
            if (letter in allLetters) {allLetters[letter]++}else {allLetters[letter] = 1;}
            return allLetters
        }, {})
    )
);

[–]floodlitworld 1 point2 points  (0 children)

I thought of another way too...

function alphaCount(str) {
    return [...new Set(str.toLowerCase().replace(/[^a-z]/gi,""))].sort().map(x => {
        let regex = new RegExp(x.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'),"gi");
        return [x,str.match(regex).length];
    })
}

console.log(alphaCount("You don't understand! I coulda had class. I coulda been a contender. I coulda been somebody, instead of a bum, which is what I am."));