title: Javascript counting the frequency letters of a string
body: <p>I've been trying to come up with an answer to this exercise, but there has to be a mistake where I can't see it. I'll share the description of the exercise, my code and finally my output. </p>
  
  <p>DESCRIPTION:</p>
  
  <p>Now we are going to save the longest length of every array in a new array called array_lengths</p>
  
  <p>Open the console and reload the index page to see the result of the code.</p>
  
  <p>Write a function charFreq() that takes a string and builds a frequency listing of the characters contained in it. </p>
  
  <p>As a condition you must use the object freqCounter.</p>
  
  <p>Represent the frequency listing as a Javascript object. Try it with something like charFreq("abbabcbdbabdbdbabababcbcbab").</p>
  
  <p>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 charFreq(string) {
  
    var array_lengths = [];
  
    // compute frequencies of each value
    for (var i = 0; i &lt; string.length; i++) {
      value = string[i];
      if (value in array_lengths) {
        array_lengths[value] ++;
      } else {
        array_lengths[value] = 1;
      }
    }
  
  
    // make array from the frequency object to de-duplicate
    var freqCounter = [];
    for (value in array_lengths) {
      freqCounter.push(value);
    }
  
    // sort the uniques array in descending order by frequency
    function compareFrequency(a, b) {
      return array_lengths[b] - array_lengths[a];
    }
  
    return freqCounter.sort(compareFrequency);
  }
  
  //OUTPUT: 
  
  // if I had it right I should get "true" from every of the following values of the function but the point is that I'm getting all false...where's the error?
  
  counter = charFreq("abbabcbdbabdbdbabababcbcbab");
  
  console.log(counter['a'] === 7);
  
  console.log(counter.b === 14);
  
  console.log(counter['c'] === 3);</code></pre>
  </div>
  </div>
  </p>