title: Count the occurrences of each element inside an array using reduce
body: <p>Count the occurrences of each element inside an array using reduce.
  My code was wrong! </p>
  
  <p>My code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function countOccurrences(arr) {
    return arr.reduce(function(a, b){
      var count = 0;
      for(var i = 0; i &lt; arr.length; i++){
        if (arr[i] == b) {
          return count + 1;
        }
      }
    }, '');
  }
  
  console.log(countOccurrences(['a', 'b', 'c', 'b', 'a']));</code></pre>
  </div>
  </div>
  </p>
  
  <p>Expect:</p>
  
  <pre><code>// { 
  //   a: 2, 
  //   b: 2, 
  //   c: 1 
  // }
  </code></pre>
  
  <p>Thanks so much!</p>