[英]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 对于前面的例子,平均值为(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. 如果将数组中的数据从字符串转换为对象,这将变得更容易。这将以两种方式使您受益:1)代码更易读,易懂,更易于维护,2)您不需要做一堆字符串体操来取出相关数据。
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. 拆分String,然后获取值并将它们转换为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. 您也不应该使用Array
构造函数。 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. 您可以使用对象构造函数而不是我使用的内联Object
,但这不是必需的。 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. 哦,使用[]
而不是new Array()
一个原因是,当你构造一个Array
,值总是真的,而[]
是假的。
I did take the liberty of correcting Bruce's last name. 我确实冒昧纠正布鲁斯的姓氏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.