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


Groups > comp.lang.javascript > #26651 > unrolled thread

function to get frequencies of a char array

Started byJRough <janis.rough@gmail.com>
First post2015-01-22 14:43 -0800
Last post2015-01-23 17:09 +0100
Articles 19 on this page of 39 — 8 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#26732

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-27 14:50 +0100
Message-ID<4289181.7vYxkYjVZ3@PointedEars.de>
In reply to#26730
Christoph M. Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Christoph M. Becker wrote:
>>> Evertjan. wrote:
>>>> This was about a count,
>>>> in Javascript you cannot
>>>> in a single variable
>>>> count from 0 to 1e32
>>>> for reasons of […] resolution.
>>>
>>> […]
>>>  However, I don't understand what you mean by "resolution".
>> 
>> [limits of IEEE 754 doubles’ integer precision]
> 
> Thanks for the explanation.

You’re welcome.

> Actually, I screwed up again, as I meant 2³² (not 10³²), which can be
> represented exactly as integer, but doesn't "survive" the ToInt32()
> call.  Even 2³¹ does not:
> 
>   Math.pow(2, 31) | 0 // => -2147483648

Correct.  I suspected something was wrong with using “| 0” there, but had 
not found the time to investigate it.  Thanks.

I have also stated that I would not use Array.prototype.reduce() to return a 
non-primitive value.  It is possible that judgement was incorrect.  It is 
rather fascinating to obtain a character (or word) statistics¹ of an HTML 
document (or any other text) – confirming, for example, that “e” usually is 
the most frequent letter –, just by typing

  document.body.textContent.split("").reduce(function (prev, curr) {
    prev[curr] = (prev[curr] || 0) + 1;
    return prev;
  }, Object.create(null));

as one line into the script console.

______
¹  it is possible to split() using a regular expression like /\s+/

> As we were talking about array *indexes*[1], values equal to or beyond
> 2³² are irrelevant, anyway.
> 
> [1] <news:2697158.aGRKFDBAEn@PointedEars.de>

I do not follow.  In

|  if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }

as cited, “e” is not an array index.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26733

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-27 19:35 +0100
Message-ID<ma8lp2$nvq$1@solani.org>
In reply to#26732
Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
>
>> As we were talking about array *indexes*[1], values equal to or beyond
>> 2³² are irrelevant, anyway.
>>
>> [1] <news:2697158.aGRKFDBAEn@PointedEars.de>
> 
> I do not follow.  In
> 
> |  if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
> 
> as cited, “e” is not an array index.

To avoid the necessity to look up your code sample, I'll post the
corrected version :

  for (var i = 0, len = a.length; i < len; ++i)
  {
    /* NOTE: hasOwnProperty() may be required */
    var e = a[i];
    if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
  }

Indeed, `e` is no array index, but `i` is.  Therefore the value of the
properties of `count` is always less than 2³² (even though it could be
greater than or equal to 2³¹).

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26734

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-27 22:15 +0100
Message-ID<2193243.aBhPuaqYS3@PointedEars.de>
In reply to#26733
Christoph M. Becker wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Christoph M. Becker wrote:
>>> As we were talking about array *indexes*[1], values equal to or beyond
>>> 2³² are irrelevant, anyway.
>>>
>> […]
>   for (var i = 0, len = a.length; i < len; ++i)
>   {
>     /* NOTE: hasOwnProperty() may be required */
>     var e = a[i];
>     if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
>   }
> 
> Indeed, `e` is no array index, but `i` is.  Therefore the value of the
> properties of `count` is always less than 2³² (even though it could be
> greater than or equal to 2³¹).

But you can have arrays and array-like objects with up to 2⁵³ elements and 
iterate over them this way:

<http://PointedEars.de/wsvn/JSX/trunk/array.js>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26762

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-31 00:40 +0100
Message-ID<mah4pq$sqt$1@solani.org>
In reply to#26734
Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
> 
>> Indeed, `e` is no array index, but `i` is.  Therefore the value of the
>> properties of `count` is always less than 2³² (even though it could be
>> greater than or equal to 2³¹).
> 
> But you can have arrays and array-like objects with up to 2⁵³ elements and 
> iterate over them this way:
> 
> <http://PointedEars.de/wsvn/JSX/trunk/array.js>

I had not considered this extraordinary possibility.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26694

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-01-24 17:49 +0000
Message-ID<87lhkskr0r.fsf@bsb.me.uk>
In reply to#26689
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>   var counts = {};
>>>
>>>   for (var i = 0, len = a.length; i < len; ++i)
>>>   {
>>>     /* NOTE: hasOwnProperty() may be required */
>>>     var e = a[i];
>>>     e ? ++counts[e] : (counts[e] = 1);
>>>   }
>> 
>> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
>> immediately below): […]
>
> No, but
>
>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }

Why?

And did you mean to add the if (e) test to the forEach examples, too?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#26701

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-26 10:23 +0100
Message-ID<XnsA42E69A74B963eejj99@194.109.133.246>
In reply to#26694
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 24 jan 2015 in 
comp.lang.javascript:

>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
> 
> Why?
> 
> And did you mean to add the if (e) test to the forEach examples, too?

That certainly would meet any irrational demand.

-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26720

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-27 01:00 +0100
Message-ID<4289032.9WYFBcEGMJ@PointedEars.de>
In reply to#26694
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>>   var counts = {};
>>>>
>>>>   for (var i = 0, len = a.length; i < len; ++i)
>>>>   {
>>>>     /* NOTE: hasOwnProperty() may be required */
>>>>     var e = a[i];
>>>>     e ? ++counts[e] : (counts[e] = 1);
>>>>   }
>>> 
>>> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
>>> immediately below): […]
>>
>> No, but
>>
>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
> 
> Why?

Because there are sparse arrays ([To]String(“undefined”) === "undefined").  
Your variant suffices for sparse arrays iterated over with the “for–in” 
statement, provided that there are no other inherited or own enumerable 
properties (otherwise .hasOwnProperty() needs to be used and the property 
name needs to be checked against the specification for Array indexes).

> And did you mean to add the if (e) test to the forEach examples, too?

No, Array.prototype.forEach() already tests internally if the numeric 
property name is an index.  That is also the reason why it is so very 
inefficient with sufficiently sparse large arrays.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26725

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-01-27 01:37 +0000
Message-ID<87twzdf1gu.fsf@bsb.me.uk>
In reply to#26720
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>> Ben Bacarisse wrote:
>>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>>>   var counts = {};
>>>>>
>>>>>   for (var i = 0, len = a.length; i < len; ++i)
>>>>>   {
>>>>>     /* NOTE: hasOwnProperty() may be required */
>>>>>     var e = a[i];
>>>>>     e ? ++counts[e] : (counts[e] = 1);
>>>>>   }
>>>> 
>>>> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
>>>> immediately below): […]
>>>
>>> No, but
>>>
>>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
>> 
>> Why?
>
> Because there are sparse arrays ([To]String(“undefined”) ===
> "undefined").

Isn't

  if (a.hasOwnProperty(i)) { counts[e] ? ++counts[e] : (counts[e] = 1); }

a better way to deal with sparse arrays in such a loop?  Testing e seems
rather heavy-handed, though it's not clear what the OP might want in
anything but the simple case given.  In a general counting loop one
would probably want to count zeros and NaNs and so on.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#26726

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-27 03:03 +0100
Message-ID<1859346.AqY2EoPZMB@PointedEars.de>
In reply to#26725
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>> Ben Bacarisse wrote:
>>>>> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>>>>>>   var counts = {};
>>>>>>
>>>>>>   for (var i = 0, len = a.length; i < len; ++i)
>>>>>>   {
>>>>>>     /* NOTE: hasOwnProperty() may be required */
>>>>>>     var e = a[i];
>>>>>>     e ? ++counts[e] : (counts[e] = 1);
>>>>>>   }
>>>>> 
>>>>> I think you meant counts[e] ? ++counts[e] : (counts[e] = 1); here (and
>>>>> immediately below): […]
>>>>
>>>> No, but
>>>>
>>>>   if (e) { counts[e] ? ++counts[e] : (counts[e] = 1); }
>>> 
>>> Why?
>>
>> Because there are sparse arrays ([To]String(“undefined”) ===
>> "undefined").
> 
> Isn't
> 
>   if (a.hasOwnProperty(i)) { counts[e] ? ++counts[e] : (counts[e] = 1); }
> 
> a better way to deal with sparse arrays in such a loop?

In general, it is a better way.  However, since Array indexes are enumerable 
properties, you can also iterate over a filtered, sorted Object.keys(a), say

  var indexes = Object.keys(a).filter(function (e) {
    return +e >= 0 && +e < Math.pow(2, 32);
  }).sort(function (a, b) {
    return a - b;
  });

  for (var ii = 0, len = indexes.length; ii < len; ++ii)
  {
    var e = a[indexes[ii]];
    // …
  }

Which approach is more efficient depends on the sparseness of the array; the 
latter solves the problem that iteration order is unspecified with “for–in” 
and includes inherited properties.

That means you could do

  var reduced = Object.keys(a).filter(…).sort(…).map(…).reduce(…);

> Testing e seems rather heavy-handed, though it's not clear what the OP 
> might want in anything but the simple case given.  In a general counting 
> loop one would probably want to count zeros and NaNs and so on.

Yes, as so often, this is limited to the use-case.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26667

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-23 15:29 +0100
Message-ID<XnsA42B9D8563ADeejj99@194.109.133.133>
In reply to#26656
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 23 jan 2015 in 
comp.lang.javascript:

> =============================
> var arr = 'abjcdjjj'.split(''); // source array
> var res = {};                   // result object
> 
> function frequencies() {
>   for(i=0; i< arr.length; i++) {
>     if (!res[arr[i]])
>       res[arr[i]] = 0;
>     res[arr[i]] +=1;
>   };
>}; 
> frequencies();  
> 
> console.log(res); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
> =============================

Some streamlining: 

=============================
var arr = 'abjcdjjj'.split(''); // source array

function frequencies() {
  var res = {}; 
  for(var i in arr)
    res[arr[i]] = (res[arr[i]]|0) + 1;
  return res;
}; 

console.log(frequencies()); // Object {a: 1, b: 1, j: 4, c: 1, d: 1}
=============================


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26657

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-01-23 02:16 +0000
Message-ID<m9satq$607$1@dont-email.me>
In reply to#26651
On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:

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

i wasn't defined with a var statement. Not a critical error, but sloppy 
if everything else is defined with var.

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

This is the wrong test. You want to test if arr[i] is a key in myMap

             if (myMap.has(i)) {

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

This isn't how you increment a value in a map. You have to retrieve the 
value properly, and it helps to terminate the line with a semi-colon. 
Additionally, do we use named arguments in javascript? As far as I know 
only in user space functions using parameter objects, and even the ES6 
support for these requires wrapping them in an object.

                myMap.set(i, myMap.get(i) + 1);

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

Oops another missing semi-colon and a named parameter. Also, you probably 
want to use a numeric 1 here for the value, not the character 1!

                myMap.set(i, 1);

>             };

Ah, that's where the semi-colons went to. But you don't need them here.

>      };

or here!

> }
> frequencies();





-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#26658

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-01-23 02:18 +0000
Message-ID<m9sb1b$607$2@dont-email.me>
In reply to#26657
On Fri, 23 Jan 2015 02:16:26 +0000, Denis McMahon wrote:

> On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:

>>         var myMap = new Map();

And as Evertjan said, this should probably come before the for loop 
starts too!

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#26659

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-23 09:06 +0100
Message-ID<XnsA42B5C9C235A0eejj99@194.109.133.133>
In reply to#26657
Denis McMahon <denismfmcmahon@gmail.com> wrote on 23 jan 2015 in 
comp.lang.javascript:

>>             };
> 
> Ah, that's where the semi-colons went to. But you don't need them here.
> 
>>      };
> 
> or here!
> 

Depending on your hour of need,
I submit they are needed just as much
for a readable code, 
showing the end of a statement.

> }

and here too.

> frequencies();



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26661

FromStefan Weiss <krewecherl@gmail.com>
Date2015-01-23 09:53 +0100
Message-ID<m9t26o$4vf$1@news.albasani.net>
In reply to#26659
On 2015-01-23 09:06, Evertjan. wrote:
> Denis McMahon <denismfmcmahon@gmail.com> wrote on 23 jan 2015 in 
> comp.lang.javascript:
> 
>>>             };
>> 
>> Ah, that's where the semi-colons went to. But you don't need them here.
>> 
>>>      };
>> 
>> or here!
>> 
> 
> Depending on your hour of need,
> I submit they are needed just as much
> for a readable code, 
> showing the end of a statement.

The end of an extra empty statement, maybe.

Blocks do not end with a semicolon - ever. Adding one, possibly with the
intention of improving readability, actually changes the code
(marginally) by creating an unnecessary empty statement.

- stefan

[toc] | [prev] | [next] | [standalone]


#26663

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-01-23 13:37 +0100
Message-ID<XnsA42B8AA5FA188eejj99@194.109.133.133>
In reply to#26661
Stefan Weiss <krewecherl@gmail.com> wrote on 23 jan 2015 in 
comp.lang.javascript:

> On 2015-01-23 09:06, Evertjan. wrote:
>> Denis McMahon <denismfmcmahon@gmail.com> wrote on 23 jan 2015 in 
>> comp.lang.javascript:
>> 
>>>>             };
>>> 
>>> Ah, that's where the semi-colons went to. But you don't need them here.
>>> 
>>>>      };
>>> 
>>> or here!
>>> 
>> 
>> Depending on your hour of need,
>> I submit they are needed just as much
>> for a readable code, 
>> showing the end of a statement.
> 
> The end of an extra empty statement, maybe.
> 
> Blocks do not end with a semicolon - ever. Adding one, possibly with the
> intention of improving readability, actually changes the code
> (marginally) by creating an unnecessary empty statement.

An unnecessary statement of you.
As I said, put a ; at the end of a statement:

if (condition) {statement;} else {statement;statement;};


-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#26664

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-23 13:49 +0100
Message-ID<m9tg0b$k77$1@solani.org>
In reply to#26663
Evertjan. wrote:

>> The end of an extra empty statement, maybe.
>>
>> Blocks do not end with a semicolon - ever. Adding one, possibly with the
>> intention of improving readability, actually changes the code
>> (marginally) by creating an unnecessary empty statement.
> 
> An unnecessary statement of you.

IBTD.

> As I said, put a ; at the end of a statement:
> 
> if (condition) {statement;} else {statement;statement;};

You can't put a semicolon at the end of block statement, because that is
not valid syntax.  Putting a semicolon *after* the end of a block
statement creates an empty statement.[1]

[1] <http://www.ecma-international.org/ecma-262/5.1/#sec-12>

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26662

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-01-23 13:31 +0100
Message-ID<m9tevk$gj3$1@solani.org>
In reply to#26657
Denis McMahon wrote:

> On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:
> 
>> function frequencies(){
>>   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>>   var key, value;
>>     for(i=0; i< arr.length; i++){
> 
> i wasn't defined with a var statement. Not a critical error, but sloppy 
> if everything else is defined with var.

It would still be sloppy, even if the other variables were not be
declared inside the function body.  IOW, avoid "implied globals".

>>         var myMap = new Map();
>>            if (arr[i]==arr[i+1]){
> 
> This is the wrong test. You want to test if arr[i] is a key in myMap
> 
>              if (myMap.has(i)) {
> 
>>               myMap.set(key=i, value= value+1)
> 
> This isn't how you increment a value in a map. You have to retrieve the 
> value properly, and it helps to terminate the line with a semi-colon. 
> Additionally, do we use named arguments in javascript? As far as I know 
> only in user space functions using parameter objects, and even the ES6 
> support for these requires wrapping them in an object.

These are not named arguments, but rather assignment expressions.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#26669

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-01-23 14:49 +0000
Message-ID<m9tn1r$uf4$1@dont-email.me>
In reply to#26657
On Fri, 23 Jan 2015 02:16:26 +0000, Denis McMahon wrote:

>              if (myMap.has(i)) {

I should have written: if (myMap.has(arr[i])) {

>                 myMap.set(i, myMap.get(i) + 1);

I should have written: myMap.set(arr[i], myMap.get(arr[i]) + 1);

>                 myMap.set(i, 1);

I should have written: myMap.set(arr[i], 1);

Working ES6 code can be found at:

http://www.sined.co.uk/tmp/jroughmap.htm

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#26671

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-01-23 17:09 +0100
Message-ID<2355442.d5ZcC854NQ@PointedEars.de>
In reply to#26657
Denis McMahon wrote:

> On Thu, 22 Jan 2015 14:43:18 -0800, JRough wrote:
>> function frequencies(){
>>   var arr = ['a', 'b', 'j','c', 'd', 'j', 'j', 'j'];
>>   var key, value;
>>     for(i=0; i< arr.length; i++){
> 
> i wasn't defined with a var statement.

A “var” statement constitutes a *declaration*, with an optional 
initialization; not a definition.

> Not a critical error,

Wrong.  Not declaring an identifier and then assigning to it causes it to be 
looked up in the entire scope chain in non-strict mode (which can lead to a 
runtime error in some environments), and a ReferenceError exception to be 
thrown in strict mode.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.javascript


csiph-web