0

I have written a simple JavaScript function to calculate an average of a dataset (see code below). I want to add the result as a new attribute to the object itself but it doesn't work. It seems that the parameter name of the function cannot be used. I don't understand why... what am I doing wrong?

var dataset01 = [{xdis:0,href:20.2},
  {xdis:16.5,href:18.3},
  {xdis:23.9,href:15.7},
  {xdis:36.1,href:13.4}
];

function calcAvg(ds,par){

    var total = 0;
    for(var i = 0; i < ds.length; i++) {
        total += ds[i][par];
    }
    var avg = total / ds.length;

    ds.avg[par]=avg;

 }

 calcAvg(dataset01,"href");
 calcAvg(dataset01,"xdis");

The error being generated

Uncaught TypeError: Cannot set property 'href' of undefined
    at calcAvg ((index):26)
    at (index):30
Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
3TW3
  • 337
  • 1
  • 3
  • 14
  • Did you check your console? You have an error that should tell you what the problem is. – Ruan Mendes Feb 08 '17 at 18:01
  • You need to include a clear error along with your code for your question to be on-topic here. – user229044 Feb 08 '17 at 18:02
  • 3
    Finger happy closers, the problem is that `ds.avg` is not defined. The question may not be perfectly asked, but the error is pretty clear – Ruan Mendes Feb 08 '17 at 18:03
  • @JuanMendes Questions *should be* closed if they don't include a clear error message. It's not up to us to figure out which error a block of code produces. – user229044 Feb 08 '17 at 18:04
  • He's using `ds.avg` as an array before he defines it... it's a simple syntax mistake. It's pretty clear what's being asked if you read the question. – Jaime Torres Feb 08 '17 at 18:04
  • 1
    @meagar It took me 2 seconds to find the problem and I was typing an answer when you closed it – Ruan Mendes Feb 08 '17 at 18:05
  • @JuanMendes I'm aware, and I also spotted the problem immediately. That doesn't make the question on-topic. – user229044 Feb 08 '17 at 18:05
  • If you truly want to assign an "average" property to an array, you have 1 of 2 choices. The first is to extend all Arrays by adding that property to the Array prototype (super bad). The second is to create your own object that can be enumerated through which includes this `avg` property that can be set and referenced. Heck, you can even add the `calcAvg` function to that to always return that value. Arrays themselves support limited functionality, calculating the average is not one of them since the data types within could be anything. – Sunny Patel Feb 08 '17 at 18:07
  • 1
    @meagar That duplicate reference to the question is not the same as what the OP is trying to accomplish. It's not relevant. – Sunny Patel Feb 08 '17 at 18:10
  • @SunnyPatel It's *identical* to what the OP is trying to accomplish. – user229044 Feb 08 '17 at 18:11
  • @meagar I don't think that is a duplicate. The problem here is that he tries to create a property to an array instance. The alleged duplicate is starting with an object `{}`. It may be an answer to this problem, but it is not technically the same question and it is surely not identical. http://www.dictionary.com/browse/identical – Marc Compte Feb 08 '17 at 18:19
  • 1
    @Marc: *"The problem here is that he tries to create a property to an array instance."* That is not the problem (unless you find assigning arbitrary properties to arrays *a* problem, which is understandable). The problem is that `ds.avg` doesn't exist. It has to be assigned an object first before `ds.avg[par]` can be assigned to. And that's the same issue as in the other question. – Felix Kling Feb 08 '17 at 18:21
  • 1
    The problem in both cases is that somebody is attempting to assign a property on an variable containing `undefined`. Whether it's `a[b][c]` or `a.b.c` or any combination there of is immaterial, and the type of `a` doesn't matter. – user229044 Feb 08 '17 at 18:21
  • `ds.avg = {}; ds.avg[par] = avg`; – Ruan Mendes Feb 08 '17 at 18:22
  • 1
    Thank you, I understand the problem now – 3TW3 Feb 08 '17 at 18:42

0 Answers0