Javascript String countCharacters()

Description

Javascript String countCharacters()


String.prototype.countCharacters = function() {
    return Array.prototype.slice.call(this).reduce(function(acc, c) {
        acc[c] = (acc[c] ||?0) + 1;/*from w  w  w.j av a  2  s.com*/
        return acc;
    }, {});
};

console.log('Hello, world!'.countCharacters());



PreviousNext

Related