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


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

get average height in an object

Started byJRough <janis.rough@gmail.com>
First post2017-11-11 14:57 -0800
Last post2017-11-13 12:28 -0800
Articles 9 on this page of 29 — 7 participants

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


Contents

  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

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


#34357 — Re: Dictionary-like objects

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-11-15 02:55 +0100
SubjectRe: Dictionary-like objects
Message-ID<3071898.QJadu78ljV@PointedEars.de>
In reply to#34342
Joao Rodrigues wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Also, why do you make up things and pretend that they are true?
> 
> You do that *all the time* - only you don't recognize the fact.
> 
>> GitHub is _not_ a version control system (Git, which GitHub uses, is).
> 
> Don't say.
> 
>>  And your claim that I would not use such is so *obviously* false
> 
>> – read my sigs.
> 
> FYI, no one cares about them.

How could you possibly know?

If you would care about them, you would not need to make up spurious claims 
about me.  My sig has been saying for a long time that I am using SVN, and 
more recently that I am providing Git repositories hosted on GitHub, too.  
From that it is evident that I am contributing to free software projects of 
others as well.  And, not to put too fine a point on it, it also says that I 
am the maintainer of the FAQ for comp.lang.javascript and the author of the 
ECMAScript Support Matrix.

You appear to have no clue whom you are talking about (or to).

>>> Anyway, putting console.log() in source code is insane and unnecessary,
>>> given all modern browsers provide "DevTools" way more capable to debug
>>> code nowadays.
>> You are dead wrong.
> 
> Maybe a little wrong, not dead wrong.

Wrong again.

>> Sometimes it is still necessary to use console.log() for debugging
>> because the interaction with the built-in debugger causes unwanted
>> results.
> 
> I challenge you to give an example of that.

Debugging keyboard and mouse events.
 
>> Also, sometimes it is useful to have console.log() & friends in *some*
>> places even if one is not actually debugging the code.  For example, if
>> you *want* to generate *messages* that only *developers* need to see.
> 
> In debug code, yes. However, using console.log() in production code is
> pure nonsense.

It is not.  People do it all the time, for the *right* reasons.  You have no 
clue what you are talking about.

>>>> Annotating “console” as an implicit global for the whole file, as you
>>>> suggested, *counteracts* this benefit.  (Except, AISB, if it is a
>>>> *debug* library.)
> 
> You are picking my comment regarding the OP's question and putting it in
> an wide context. What is the name for this fallacy?

It is not a fallacy.  Your knee-jerk suggestion is unwise at best.  As I 
indicated, the logical course of action, *if* JSLint is actually a problem, 
is to use something like JSHint, to use console.log() only in the right 
places, and then use JSHint & Co.’s refined annotation.  Even the OP seem
to have realized that.
 
>>>>> (2) an obsolete and opinionated linter such as JSLint.
>>>> The same is true for other linters *by default*, so that argument does
>>>> not hold water.
>>> Nonsense.
>> 
>> It is not nonsense.  *For example*, JSHint will by default produce a
>> warning on the use of “console”, too, because it does not need to be
>> defined in order to be used; so that is not at all “opinionated” of
>> JSLint.
> 
> You are missing the point. JSLint is opinionated and everyone knows
> about that,

Especially the silent majority which agrees with you :->

<https://en.wikipedia.org/wiki/Argumentum_ad_populum>

> except (maybe) his creator. But you are trying to distort my words
> conveniently in favour of your arguments.

Your logic is flawed.  That JSLint may be “opinionated” on certain points 
does not change the fact that it is a good idea to point out the unintended 
use of “console” and other environment-specific global symbols.
 
>>      [If you want to remember, you had made that argument yourself; I am
>>       merely repeating it.  Unfortunately, one can observe here again
>>       that you "forget" your own arguments when they become inconvenient
>>       for you.]
> 
> You do that *all the time*.

Liar.  The record shows that I am the first to admit when I have been 
*showed* to be wrong.
 
>> However, maybe by contrast to JSLint, JSHint provides several ways in
>> which to disable that warning in *some* places where the use of “console”
>> was *intentional*.
> 
> My suggestion for using the JSLint global directive would affect only
> the file in which it appears - the OP's example. Nonetheless, you are
> trying to spread panic around such a trivial fact as if it was the end
> of the world.

Nonsense.  You do not know the full content of the OP’s file.

>>> That was hilarious indeed.
>> It was.  Your overconfident ignorance is most refreshing at times.
>> Have you ever heard of the Dunning–Kruger effect?
> 
> Again, one can observe your customary behaviour in using /ad hominem/
> fallacy against everyone that "dares" to thwart your arguments.

<https://en.wikipedia.org/wiki/Dunning–Kruger_effect>

>> Here is another one:
>> 
>>    /* somewhere in code that the OP’s code uses */
>>    Object.prototype.answer = 42;
> 
> Extending Object.prototype is generally a bad idea, but extending it
> with enumerable properties is even worse. I would never expect that from
> you.

Straw man.  You really need to read more carefully.

> I had enough. Bye.

“I am out of arguments but I am afraid to admit 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.

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


#34358 — Re: Dictionary-like objects

FromJoao Rodrigues <groups_jr-1@yahoo.com>
Date2017-11-15 12:30 -0200
SubjectRe: Dictionary-like objects
Message-ID<ouhj27$joi$1@gioia.aioe.org>
In reply to#34357
Thomas 'PointedEars' Lahn wrote:

>>> It was.  Your overconfident ignorance is most refreshing at times.
>>> Have you ever heard of the Dunning–Kruger effect?
>>
>> Again, one can observe your customary behaviour in using /ad hominem/
>> fallacy against everyone that "dares" to thwart your arguments.
> 
> <https://en.wikipedia.org/wiki/Dunning–Kruger_effect>

I am happy that you have been searching for help in Wikipedia.

> 
>>> Here is another one:
>>>
>>>     /* somewhere in code that the OP’s code uses */
>>>     Object.prototype.answer = 42;
>>
>> Extending Object.prototype is generally a bad idea, but extending it
>> with enumerable properties is even worse. I would never expect that from
>> you.
> 
> Straw man.  You really need to read more carefully.

I agree with you on the hasOwnProperty() check within for...in loops, if 
it makes you feel good.

>> I had enough. Bye.
> 
> “I am out of arguments but I am afraid to admit it.”
>   

No, I am just bored to death with you rambling on and on.

-- 
Joao Rodrigues

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


#34360 — Re: Dictionary-like objects

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2017-11-15 15:59 +0000
SubjectRe: Dictionary-like objects
Message-ID<82po0dttqg3gefjpoo8gtgba4e5grgoac2@4ax.com>
In reply to#34357
On Wed, 15 Nov 2017 02:55:50 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

  <snip>
>Liar.  The record shows that I am the first to admit when I have been 
>*showed* to be wrong.
  <snip>

Untrue.

  John

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


#34340

FromJRough <janis.rough@gmail.com>
Date2017-11-13 12:08 -0800
Message-ID<42753fd7-b4cb-44fe-a173-081db5aa8f3a@googlegroups.com>
In reply to#34329
On Sunday, November 12, 2017 at 6:26:24 PM UTC-8, Joao Rodrigues wrote:
> Thomas 'PointedEars' Lahn wrote:
> > 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.
> 
> Useless remark since I wrote about a JSLint warning. It has nothing to 
> do with libraries whatsoever. Don't miss the point.
> 
> >   
> >> 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().
> 
> Of course ESLint, JSHint and JSLint warn about undeclared use of 
> 'console' and its methods. JS Linters 101.
> 
> >   
> >>> 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.  
> 
> Nonsense. If semicolons were really optional there would not be a thing 
> called "Automatic Semicolon Insertion" (ASI) in the Specification, which 
> states:
> 
> "§ 11.9 - Automatic Semicolon Insertion
> Certain ECMAScript statements (empty statement, let, const, import, and 
> export declarations, variable statement, expression statement, debugger 
> statement, continue statement, break statement, return statement, and 
> throw statement) *must be terminated with semicolons*."
> 
> Notice the *must be terminated with semicolons* sentence.
> 
> <http://www.ecma-international.org/ecma-262/6.0/#sec-automatic-semicolon-insertion>
> 
> Omitting semicolons does not mean they are optional in the Language. 
> Look up the Standard and you will not find the term "optional" for 
> semicolons.
> 
> ECMAScript Language Specification 101.
> 
> >>>
> >>> const data = {
> >>> "Matt" : {  "height" : 174, "weight": 69 },
> >>> "Jason" : { "height" : 190, "weight": 103 }
> >>> }
> >>
> >> It lacks a semicolon after the "}".
> 
> I was pointing out the position in the OP's source text where they 
> reported the JSLint warning. So the comment above is valid in the 
> context of the OP's question. You are missing the point.
> 
> >   
> >> 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.
> 
> It sure does. You are missing the point again.
> 
> > 
> >> 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
> 
> Prototypal inheritance is a known concept by everyone here. However, you 
> have *intentionally* omitted the fact that the for...in loop statement 
> iterates over enumerable properties of an Object, and that's precisely 
> the point here: the hasOwnProperty method is irrelevant for iterating 
> the Object referenced by the "data" constant because it hasn't got 
> enumerable properties inherited from Object in the OP's example.
> 
> 
> > 
> > | -> 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
> >                                                     ^^^^^^^^
> 
> You are missing the point again and again.
> 
> > 
> > Next time, try it out before you post.
> >   
> 
> Next time, read carefully the OP and subsequent answers before posting 
> drivel.
> 
> -- 
> Joao Rodrigues

to get around changing the lint preferences you can do window.console.log and it resolves.

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


#34334

FromJohn G Harris <niam@jghnorth.org.uk.invalid>
Date2017-11-13 10:17 +0000
Message-ID<k9si0ddpeef71nabf38709itonqekcdrhl@4ax.com>
In reply to#34322
On Sun, 12 Nov 2017 18:24:27 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

  <snip>
>The value of the “data” constant *variable* here is a reference to an Object 
>instance 
  <snip>

When an object is assigned to a property or variable there will be a
pointer of some kind involved. The question is this : Is that pointer
inside or outside the Value of the property or variable?

The Java specification says clearly [1] that the pointer is inside the
Value. 

The ECMAScript standard says clearly [2] that the pointer is outside
the Value.

It is therefore totally incorrect when talking about ECMAScript to say
that 
  "The value ... is a reference to an Object instance".


[1] The Java Language Specification, Java SE 8 Edition, sec 4.3.1
[2] ECMA 262, v8, sec 6.1

  John

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


#34339

FromJRough <janis.rough@gmail.com>
Date2017-11-13 12:07 -0800
Message-ID<facbd344-c431-473d-b264-6b026ba7021d@googlegroups.com>
In reply to#34322
On Sunday, November 12, 2017 at 9:24:34 AM UTC-8, Thomas 'PointedEars' Lahn wrote:
> 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.

thanks for the JShint links.  I switched to that one.

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


#34314

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-11-12 11:00 +0100
Message-ID<XnsA82B6FE8B7C01eejj99@194.109.6.166>
In reply to#34309
JRough <janis.rough@gmail.com> wrote on 11 Nov 2017 in
comp.lang.javascript: 

> 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 and it says it needs a semi colon after the data 
> object?  In any case, maybe I did something wrong with the second for
> loop where I am only trying to collect the height property.  I am not
> exactly sure how to get the height property from the data object.  The
> result of running this script is undefined.  thanks, 
> 
> const data          ={ 
>      "Matt" : {  "height" : 174, "weight": 69 },
>      "Jason" : { "height" : 190, "weight": 103 } 
>}
> function calcAvgHeight(data) { 
> "use strict";
> var count = '';
> var avgHeight = '';
> var result = [];
> for (var key in data){
>      if (!data.hasOwnProperty(key)){
>           return;
>     }
>      var obj = data[key];
>      for (var height in obj){
>           if(!obj.hasOwnProperty.height){
>                return;
>           }
>      
>                       console.log(obj[height]);
>                      result.push(obj[height]);
>                      count += 1;
>                      avgHeight = result/count;
>                      console.log(avgHeight);
>                          return avgHeight ;  
>     }
>    }
>}
> calcAvgHeight(data);
> 

<script>
'use strict';
let total = 0, numb = 0;
const data = { 
	"Matt" : {  "height" : 174, "weight": 69 },
	"Jason" : { "height" : 190, "weight": 103 } 
};

Object.keys(data).forEach( 
	(key) => {
		numb++;
		total += data[key]['height'];
		}
	);

console.log('total height = ' + total, 
	', number of heights = ' + numb,
	', average height = ' + total/numb);

</script>


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

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


#34316

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2017-11-12 11:15 +0100
Message-ID<XnsA82B728564145eejj99@194.109.6.166>
In reply to#34314
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 12 Nov 2017 in 
comp.lang.javascript:

> JRough <janis.rough@gmail.com> wrote on 11 Nov 2017 in
> comp.lang.javascript: 
> 
>> 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 and it says it needs a semi colon after the data 
>> object?  In any case, maybe I did something wrong with the second for
>> loop where I am only trying to collect the height property.  I am not
>> exactly sure how to get the height property from the data object.  The
>> result of running this script is undefined.  thanks, 
>> 
>> const data          ={ 
>>      "Matt" : {  "height" : 174, "weight": 69 },
>>      "Jason" : { "height" : 190, "weight": 103 } 
>>}
>> function calcAvgHeight(data) { 
>> "use strict";
>> var count = '';
>> var avgHeight = '';
>> var result = [];
>> for (var key in data){
>>      if (!data.hasOwnProperty(key)){
>>           return;
>>     }
>>      var obj = data[key];
>>      for (var height in obj){
>>           if(!obj.hasOwnProperty.height){
>>                return;
>>           }
>>      
>>                       console.log(obj[height]);
>>                      result.push(obj[height]);
>>                      count += 1;
>>                      avgHeight = result/count;
>>                      console.log(avgHeight);
>>                          return avgHeight ;  
>>     }
>>    }
>>}
>> calcAvgHeight(data);
>> 
> 
> <script>
> 'use strict';
> let total = 0, numb = 0;
> const data = { 
>      "Matt" : {  "height" : 174, "weight": 69 },
>      "Jason" : { "height" : 190, "weight": 103 } 
>};
> 
> Object.keys(data).forEach( 
>      (key) => {
>           numb++;
>           total += data[key]['height'];
>           }
>      );
> 
> console.log('total height = ' + total, 
>      ', number of heights = ' + numb,
>      ', average height = ' + total/numb);
> 
> </script>
> 

Or showing intermediate results:

Object.keys(data).forEach( 
	(key) => {
		numb++;
		total += data[key]['height'];
		console.log('total height = ' + total, 
			', number of heights = ' + numb,
			', average height = ' + total/numb);
		}
	);




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

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


#34341

FromJRough <janis.rough@gmail.com>
Date2017-11-13 12:28 -0800
Message-ID<bfa75901-b6b9-43bd-bd85-ac422bb96c7d@googlegroups.com>
In reply to#34314
On Sunday, November 12, 2017 at 2:00:41 AM UTC-8, Evertjan. wrote:
> JRough <janis.rough@gmail.com> wrote on 11 Nov 2017 in
> comp.lang.javascript: 
> 
> > 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 and it says it needs a semi colon after the data 
> > object?  In any case, maybe I did something wrong with the second for
> > loop where I am only trying to collect the height property.  I am not
> > exactly sure how to get the height property from the data object.  The
> > result of running this script is undefined.  thanks, 
> > 
> > const data          ={ 
> >      "Matt" : {  "height" : 174, "weight": 69 },
> >      "Jason" : { "height" : 190, "weight": 103 } 
> >}
> > function calcAvgHeight(data) { 
> > "use strict";
> > var count = '';
> > var avgHeight = '';
> > var result = [];
> > for (var key in data){
> >      if (!data.hasOwnProperty(key)){
> >           return;
> >     }
> >      var obj = data[key];
> >      for (var height in obj){
> >           if(!obj.hasOwnProperty.height){
> >                return;
> >           }
> >      
> >                       console.log(obj[height]);
> >                      result.push(obj[height]);
> >                      count += 1;
> >                      avgHeight = result/count;
> >                      console.log(avgHeight);
> >                          return avgHeight ;  
> >     }
> >    }
> >}
> > calcAvgHeight(data);
> > 
> 
> <script>
> 'use strict';
> let total = 0, numb = 0;
> const data = { 
> 	"Matt" : {  "height" : 174, "weight": 69 },
> 	"Jason" : { "height" : 190, "weight": 103 } 
> };
> 
> Object.keys(data).forEach( 
> 	(key) => {
> 		numb++;
> 		total += data[key]['height'];
> 		}
> 	);
> 
> console.log('total height = ' + total, 
> 	', number of heights = ' + numb,
> 	', average height = ' + total/numb);
> 
> </script>
> 
> 
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

thanks, nice use of foreach and arrow functions.

[toc] | [prev] | [standalone]


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

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


csiph-web