Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26652

Re: function to get frequencies of a char array

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: function to get frequencies of a char array
Date 2015-01-22 23:09 +0000
Organization A noiseless patient Spider
Message-ID <877fwetntb.fsf@bsb.me.uk> (permalink)
References <dec04bcb-c8c2-4446-b82c-6c3fa7e05e48@googlegroups.com>

Show all headers | View raw


JRough <janis.rough@gmail.com> writes:

> the output should be:
> a:1,b:1,j:4,c:1,d:1

The code does no output.  You mean these should be the value in the map,
yes?

> I am trying to use a map and use the keys as the char and the values
> as the count but I'm getting undefined.  thanks,

This looks like coursework, so my intuition is just to ask some
questions that I hope will lead you to an answer.

> function frequencies(){
>   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>   var key, value;
>     for(i=0; i< arr.length; i++){

You should declare i as well.  You can do it in the for statement.

>         var myMap = new Map();
>            if (arr[i]==arr[i+1]){

This will access beyond the end of the array and I can't see what it has
to do with counting.  You want to set the map entry to 1 when you see a
letter for the first time, not when you see a letter that is different
to the one that follows it.

The key thing here is that you get the value out of the map.  That is
what tells you if you have seen this letter before.

>               myMap.set(key=i, value= value+1)

value has not been set.  value = value + 1 sets value to one mode than
undefined.  Not what you mean at all.

>             }else{
>               myMap.set(key=i,"1")

Why "1" rather than 1?  Why key=i rather than i?  Is i even the right
key?

>             };  
>      };

The function returns nothing, so it has no point.

> } 
> frequencies();  

-- 
Ben.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 14:43 -0800
  Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-22 23:09 +0000
    Re: function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 15:22 -0800
  Re: function to get frequencies of a char array JRough <janis.rough@gmail.com> - 2015-01-22 15:23 -0800
  Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 00:36 +0100
    Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 14:27 +0100
      Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-23 16:52 +0100
        Re: function to get frequencies of a char array Scott Sauyet <scott.sauyet@gmail.com> - 2015-01-23 09:03 -0800
        Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 23:03 +0100
        Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-24 14:29 +0000
          Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-24 15:36 +0100
            Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-24 16:06 +0100
              Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-24 18:05 +0100
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 10:21 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-26 13:22 +0100
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 15:50 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-26 20:48 +0100
                Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 23:08 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 01:05 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-27 13:55 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 14:50 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-27 19:35 +0100
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 22:15 +0100
                Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-31 00:40 +0100
            Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-24 17:49 +0000
              Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-26 10:23 +0100
              Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 01:00 +0100
                Re: function to get frequencies of a char array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-27 01:37 +0000
                Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-27 03:03 +0100
    Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 15:29 +0100
  Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 02:16 +0000
    Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 02:18 +0000
    Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 09:06 +0100
      Re: function to get frequencies of a char array Stefan Weiss <krewecherl@gmail.com> - 2015-01-23 09:53 +0100
        Re: function to get frequencies of a char array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-01-23 13:37 +0100
          Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 13:49 +0100
    Re: function to get frequencies of a char array "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-01-23 13:31 +0100
    Re: function to get frequencies of a char array Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-23 14:49 +0000
    Re: function to get frequencies of a char array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-23 17:09 +0100

csiph-web