简体   繁体   中英

Calculate the average of points in a array - Javascript

I have an array with infos about a group of people : name, current status, new points, last event points

Example:

var group = new Array();
group[0] = "John Doe,beginer,14,7";
group[1] = "Lois Lane,advanced,13,9";
group[2] = "Bruce Waine,advanced,17,10";

I need a function that calculates the average of the new points.

For the previous example the average would be (14+13+17)/3 = 14.66666666666667

It'd be a heck of a lot easier if you convert the data in the array from strings to objects This will benefit you in two ways: 1) the code will be more readable, understandable, and easier to maintain, and 2) you won't have to do a bunch of string gymnastics to pull out the relevant data.

Do something like this:

var group = [
  { name: 'John Doe', status: 'beginner', newPoints: 14, eventPoints: 7 },
  { name: 'Lois Lane', status: 'advanced', newPoints: 13, eventPoints: 9 },
  { name: 'Bruce Waine', status: 'advanced', newPoints: 17, eventPoints: 10 }
];

function getAverageNewPoints(people) {
  var count = people.length || 0,
      average = 0;

  for (var i = 0; i < count; i++) {
    average += people[i].newPoints;
  }

  return average / count;
}

alert('The average of new points in the group is: ' + getAverageNewPoints(group));

Try the following:

function groupAverage(group) {
    var sum = 0;
    var count = group.length;

    for (var i in group) {
        sum += parseInt(group[i].split(',')[2], 10);
    }

    return sum / count;
}

Split the String at , and get the values and convert them to Number.

var group = new Array();
 group[0] = "John Doe,beginer,14,7";
 group[1] = "Lois Lane,advanced,13,9";
 group[2] = "Bruce Waine,advanced,17,10";
sum=0;

for(var i in group)
{
    sum=sum+Number(group[i].split(",")[2]);

}

console.log(sum/group.length);  

You have a bad data structure for this. You don't want to use strings. You also should not use the Array constructor. Start with:

var group = [
    {name: "John Doe",    rank: "beginner", points: 14, lastScore: 7},
    {name: "Lois Lane",   rank: "advanced", points: 13, lastScore: 9},
    {name: "Bruce Wayne", rank: "advanced", points: 17, lastScore: 10},
],
length = group.length,
sum = 0,
i;

for ( i = 0; i < length; ++i) {
    sum += group[i].points;
}

return sum / length; // Or do something else with the result.
                     // I did no error checking.

You could use an object constructor instead of the inline Object I used, but that's not really necessary. I'm just curious; did you use strings as a default, or was using a string interface part of a textbook assignment?

Oh, one reason to use [] instead of new Array() is that when you construct an Array , the value is always truthy, while [] is falsy.

I did take the liberty of correcting Bruce's last name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM