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


Groups > comp.lang.javascript > #34322

Re: get average height in an object

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: get average height in an object
Date 2017-11-12 18:24 +0100
Organization PointedEars Software (PES)
Message-ID <4335673.31r3eYUQgx@PointedEars.de> (permalink)
References <d68354d4-3603-4201-9c12-a51977c9cf9b@googlegroups.com> <ou8vaq$1l44$1@gioia.aioe.org>

Show all headers | View raw


Joao Rodrigues wrote:

> JRough wrote:
>> This seems like it should be easy enough but I don't get any error
>> messages in the console.  I used jslint and it objects to the console.log
>> lines
> 
> This happens because "console" is a browser (and Node.js) feature.
> However, you may use the /*global*/ directive to "hide" the error in
> JSLint [1]. For instance:
> 
> /*global console */

Too sweeping, unless we are talking about a dedicated debug library.
 
> As a side note there are better JS linters such as ESLint and JSHint.

JSHint (which I prefer) will also warn about the undeclared use of 
console.log().

With JSHint at least, it is then better to annotate the corresponding 
statement only so as to suppress the *warning* temporarily –

<http://jshint.com/docs/#inline-configuration>

– and to not leave any *unintended* debug statements in *production* code.

<http://jshint.com/docs/options/#globals> p.
<http://jshint.com/docs/options/#undef>
<http://jshint.com/docs/options/#devel>
<http://jshint.com/docs/options/#node>
 
>> and it says it needs a semi colon after the data  object?
> 
> Sure does. A semicolon is required in the end of a statement.

No, in general it is not.  ECMAScript implementations insert semicolons 
automatically according to the grammar.  However, because that can have 
unintentional results, and because of readability, writing semicolons 
explicitly is *recommended*, considered good code style (and should be 
contained in any style manual).

That would be taught in “JavaScript 101”.

> The result
>> 
>> const data = {
>> "Matt" : {  "height" : 174, "weight": 69 },
>> "Jason" : { "height" : 190, "weight": 103 }
>> }
> 
> It lacks a semicolon after the "}".

Nonsense:

| -> const data = {
|   "Matt" : {  "height" : 174, "weight": 69 },
|   "Jason" : { "height" : 190, "weight": 103 }
| }
| <- undefined
| 
| -> data
| <- Object {Matt: Object, Jason: Object}
 
> I've refactored the OP's code to:
> 
> const data = {
>    "Matt" : {  "height" : 174, "weight": 69 },
>    "Jason" : { "height" : 190, "weight": 103 }
> };
> 
> function calcAvgHeight(data) {
>    var count = 0, sumHeight = 0;
>    for (var key in data) {
>      sumHeight += data[key].height;
>      count += 1;
>    }
>    return sumHeight/count;
> }
>  
> console.log(calcAvgHeight(data)); // 182

And unsurprisingly this compiles and executes without any semicolons and 
quote characters as well.

> You don't really need to check for data.hasOwnProperty(key) in this
> case, because data is a simple dictionary-like object [2].

Nonsense, and your source does not corroborate your claim.

The value of the “data” constant *variable* here is a reference to an Object 
instance which inherits properties from the object referred to by the 
initial value of Object.prototype:

| -> const o = {};
| <- undefined
| 
| -> typeof o.toString
| <- "function"
| 
| -> o.foo = "bar"
| <- "bar"
| 
| -> o
| <- Object {foo: "bar"}
| 
| -> o = 42
| <- |> Uncaught TypeError: Assignment to constant variable.  VM1150:1
                                                   ^^^^^^^^
|         at <anonymous>:1:3

(That, too, would be taught in “JavaScript 101”.)

IOW, that a value has been assigned to an identifier that has been declared 
with “const” does *not* in general mean that the data referred to by the 
identifier cannot change.  It means that the identifier cannot be assigned 
to *again*, so *only* if its value is *primitive* it cannot be changed.

In order to create a true “dictionary-like object”, one has to use

  var o = Object.create(null, …);

or

  const o = Object.create(null, …);

or equivalent [see jsx.object.getDataObject() in my JSX:object.js and, more 
recently, jsx.object.createDataObject()¹ in the same file].

In order to create immutable objects, one also needs to use Object.seal() or 
Object.freeze() or equivalent.

<http://www.ecma-international.org/ecma-262/8.0/#sec-properties-of-the-object-constructor>

> [2]
> <https://stackoverflow.com/questions/41539249/when-is-hasownproperty-not-required>

Next time, try it out before you post.
 
______
¹ The method is not a getter but a factory, hence the renaming; by using
  function references, the older identifier remains an alias until I have
  refactored all the code that uses it.
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

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


Thread

get average height in an object JRough <janis.rough@gmail.com> - 2017-11-11 14:57 -0800
  Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-12 06:04 -0200
    Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-12 06:49 -0200
    Re: get average height in an object John G Harris <niam@jghnorth.org.uk.invalid> - 2017-11-12 13:39 +0000
    Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-12 13:40 -0200
      Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:06 -0800
      Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-15 13:14 -0200
        Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-15 22:21 +0100
          Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-15 22:57 +0100
            Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-16 02:46 -0200
              Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-17 13:06 +0100
    Re: get average height in an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-12 18:24 +0100
      Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-13 00:26 -0200
        Re: get average height in an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-13 03:42 +0100
          Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-13 01:23 -0200
            Re: get average height in an object Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-13 14:16 +0100
              Re: get average height in an object Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2017-11-13 14:20 -0300
                Dictionary-like objects (was: get average height in an object) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-13 19:56 +0100
                Re: Dictionary-like objects Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2017-11-13 20:26 -0200
                Re: Dictionary-like objects Julio Di Egidio <julio@diegidio.name> - 2017-11-13 15:48 -0800
                Re: Dictionary-like objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-11-15 02:55 +0100
                Re: Dictionary-like objects Joao Rodrigues <groups_jr-1@yahoo.com> - 2017-11-15 12:30 -0200
                Re: Dictionary-like objects John G Harris <niam@jghnorth.org.uk.invalid> - 2017-11-15 15:59 +0000
        Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:08 -0800
      Re: get average height in an object John G Harris <niam@jghnorth.org.uk.invalid> - 2017-11-13 10:17 +0000
      Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:07 -0800
  Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-12 11:00 +0100
    Re: get average height in an object "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-12 11:15 +0100
    Re: get average height in an object JRough <janis.rough@gmail.com> - 2017-11-13 12:28 -0800

csiph-web