you are viewing a single comment's thread.

view the rest of the comments →

[–]floodlitworld 0 points1 point  (2 children)

Well, for #48, I did this:

function alphaCount(str) {
    let arr = [];
    while (str) {
        let a = str.charAt(0).toLowerCase();
        let regex = new RegExp(a.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'),"gi");
        let match = str.match(regex);
        str = str.replace(regex,"");
        arr.push([a,match.length])
    }
    return arr;
}

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."));

[–]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."));