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


Groups > comp.lang.javascript > #34341

Re: get average height in an object

Newsgroups comp.lang.javascript
Date 2017-11-13 12:28 -0800
References <d68354d4-3603-4201-9c12-a51977c9cf9b@googlegroups.com> <XnsA82B6FE8B7C01eejj99@194.109.6.166>
Message-ID <bfa75901-b6b9-43bd-bd85-ac422bb96c7d@googlegroups.com> (permalink)
Subject Re: get average height in an object
From JRough <janis.rough@gmail.com>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous 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