-1

I'm trying to calculate the average temperature of one week, but I do not quite know how I would do this. I've tried out some things but the end result would be either 'NaN' or 'Infinity'. Definitely doing something wrong here..

Here's the code I need to work with:

var temperatures;

temperatures = new Array();

temperatures["monday"] = 23.5;
temperatures["tuesday"] = 22.3;
temperatures["wednesday"] = 28.5;
temperatures["thursday"] = 23.5;
temperatures["friday"] = 22.3;
temperatures["saturday"] = 28.5;
temperatures["sunday"] = 29.5;

I got it working when the arrays were like [0], [1] instead of Strings containing the days, but I don't know how to do it like above. Also if you have any suggestions please try to keep the code basic as surprisingly enough 'advanced code' isn't too appreciated in my class for some reason.

Thanks for reading.

laaposto
  • 11,835
  • 15
  • 54
  • 71
Hooded Ent
  • 21
  • 1
  • 2
  • It would be helpful if you showed the code you were currently using to get the average, even if its returning the wrong result. – symcbean Sep 19 '14 at 09:23
  • It seems to be already explained here: http://stackoverflow.com/questions/10359907/array-sum-and-average – billrousseau Sep 19 '14 at 09:24
  • 1
    You're mixing arrays (accessing values by integer index) and objects (accessing values by key) – Volune Sep 19 '14 at 09:26

4 Answers4

3

The average is just the total divided by number of temperatures

var temperatures = {},
    length = 0,
    total  = 0;

temperatures["monday"] = 23.5;
temperatures["tuesday"] = 22.3;
temperatures["wednesday"] = 28.5;
temperatures["thursday"] = 23.5;
temperatures["friday"] = 22.3;
temperatures["saturday"] = 28.5;
temperatures["sunday"] = 29.5;

for (var day in temperatures) {
    total += temperatures[day];
    length++;
}

var average = total / length;

Note that arrays don't have named keys, only objects do

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • isn't `day` a global variable now? (I'm not sure - it's just what my Netbeans code highlighter is showing) If so, wouldn't it be better to also declare it with the other vars on top? – Cold_Class Jun 30 '17 at 21:19
  • 1
    @Cold_Class - good eyes, just declaring it in the for loop solves that. – adeneo Jun 30 '17 at 21:27
0

Try:

 var temperatures;
    var sum = 0,
        count = 0;
    temperatures = new Array();
    
    temperatures["monday"] = 23.5;
    temperatures["tuesday"] = 22.3;
    temperatures["wednesday"] = 28.5;
    temperatures["thursday"] = 23.5;
    temperatures["friday"] = 22.3;
    temperatures["saturday"] = 28.5;
    temperatures["sunday"] = 29.5;
    
    for (var prop in temperatures) {
        if (temperatures.hasOwnProperty(prop)) {
            count++;
            sum += temperatures[prop];
        }
    }

    var average = (sum / count);
    alert(average);
laaposto
  • 11,835
  • 15
  • 54
  • 71
0

You should try Object instead of Array, if you wish Key Value pair.

var temperatures;

temperatures = new Object();

temperatures["monday"] = 23.5;
temperatures["tuesday"] = 22.3;
temperatures["wednesday"] = 28.5;
temperatures["thursday"] = 23.5;
temperatures["friday"] = 22.3;
temperatures["saturday"] = 28.5;
temperatures["sunday"] = 29.5;

alert(temperatures["sunday"]);

you will get key value pair js object, you can use it for Average:

var sum = 0;
for(key in temperatures)
{
   sum  = sum+temperatures[key];
   console.log(temperatures[key]);
}
alert(sum/7); 

Check this Demo

bharatpatel
  • 1,203
  • 11
  • 22
  • 1
    What you trying to achieve. Have you read question carefully? – Manwal Sep 19 '14 at 09:34
  • @Manwal Sorry for the answer without example, but i guess questioner wants to access variable with key instead of index, and do average of that.Now i had updated my answer with Demo. – bharatpatel Sep 19 '14 at 09:50
0

I believe traversing causing problem for you. here is solution

DEMO

var count=0,total=0;
for(t in temperatures)
{
    total = total+temperatures[t]
    count++;
}
alert(total/count);//average
Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93