Defoe
Defoe

Reputation: 371

Javascript counting the frequency letters of a string

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.

DESCRIPTION:

Now we are going to save the longest length of every array in a new array called array_lengths

Open the console and reload the index page to see the result of the code.

Write a function charFreq() that takes a string and builds a frequency listing of the characters contained in it.

As a condition you must use the object freqCounter.

Represent the frequency listing as a Javascript object. Try it with something like charFreq("abbabcbdbabdbdbabababcbcbab").

CODE:

function charFreq(string) {

  var array_lengths = [];

  // compute frequencies of each value
  for (var i = 0; i < 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);

Upvotes: 2

Views: 4572

Answers (4)

Joe
Joe

Reputation: 82654

You are usings an Array as an Object I left var freqCounter = Object.keys(array_lengths); because it is the unique characters from the string. However, it doesn't do anything for you in the code you described.

function charFreq(string) {

  var array_lengths = {};
  var value;
  // compute frequencies of each value
  for (var i = 0; i < 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 = Object.keys(array_lengths);

  // sort the uniques array in descending order by frequency
  function compareFrequency(a, b) {
    return array_lengths[b] - array_lengths[a];
  }

  return array_lengths;
}

//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);
console.log(counter['a'] === 7);

console.log(counter.b === 14);

console.log(counter['c'] === 3);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386868

Two problems

  • use an object

    var array_lengths = {}; // object
    
  • return that object without sorting and other stuff

    return array_lengths;
    

function charFreq( string ) {
    var array_lengths = {}; // object

    // compute frequencies of each value
    for(var i = 0; i < string.length; i++) {
        value = string[i];
        if(value in array_lengths) {
            array_lengths[value]++;
        } else {
            array_lengths[value] = 1;
        }
    }
    return array_lengths;
}

//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);
console.log( counter['a'] === 7);
console.log( counter.b === 14);
console.log( counter['c'] === 3);

Upvotes: 5

Jeremy Jackson
Jeremy Jackson

Reputation: 2257

Break it down. You need to iterate over each part of the string, right? Split it into an array and create a counter for each element in an object:

var counter = {};
var string = "abbabcbdbabdbdbabababcbcbab";
var stringArray = string.split('');
stringArray.forEach(function(character){
  if(!counter.hasOwnProperty(character)){
    counter[character] = 0;
  }
  counter[character]++;
});
console.log(counter);

Upvotes: 1

Rob M.
Rob M.

Reputation: 36541

You can do this using ES6 - use Set to get the unique items in an array by converting your string to an array and passing the entire thing to new Set

let string = 'abbabcbdbabdbdbabababcbcbab';
let unique = [...new Set(string)];

Upvotes: 0

Related Questions