str.countLetters()
// âď¸ stringCountLetters()
// 'abcc' => { a: 1, b: 1, c: 2 }
// âď¸âď¸ reduce() ćŻ for loop 忍ĺ
Šĺâď¸âď¸
function stringCountLetters(str) {
return str
.split('')
.reduce((dict, c) => (dict[c] = (dict[c] || 0) + 1, dict) , {});
}
// âď¸ str.countLetters()
String.prototype.countLetters = function() {
return stringCountLetters(this);
};
Last updated
Was this helpful?