# str.countLetters()

{% tabs %}
{% tab title="JS" %}
```javascript
// ⭐️ 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);
};

```
{% endtab %}

{% tab title="📗 參考" %}
*
{% endtab %}

{% tab title="💾 程式" %}
* codepen ⟩ string.[countLetters](https://codepen.io/lochiwei/pen/Vwzvqmw?editors=0012)() - 裡面有**不同方法**的**跑分**。
* codewars ⟩ [Scramblies](https://www.codewars.com/kata/55c04b4cc56a697bb0000048/train/javascript) ([my solution](https://www.codewars.com/kata/reviews/55c04c11aa1e89cf9f000185/groups/616913b6fb13c9000100a98c))
{% endtab %}
{% endtabs %}
