Lonlon
Posts: 0
Joined: Sat Jun 14, 2014 7:04 am

Multiple computation in array of data

I have a table of scores, I want to get the total scores per Contestant and per Criteria. Any body knows the server code of these scores? Please help. Please see the target output below. Thank you.

TABLE
Score Contestant Criteria Judges
30 Contestant #1 Confident Judge #1
15 Contestant #1 Costume Judge #1
40 Contestant #1 Talent Judge #1
28 Contestant #1 Confident Judge #2
12 Contestant #1 Costume Judge #2
40 Contestant #1 Talent Judge #2

34 Contestant #2 Confident Judge #1
20 Contestant #2 Costume Judge #1
44 Contestant #2 Talent Judge #1
33 Contestant #2 Confident Judge #2
19 Contestant #2 Costume Judge #2
44 Contestant #2 Talent Judge #2

35 Contestant #3 Confident Judge #1
18 Contestant #3 Costume Judge #1
42 Contestant #3 Talent Judge #1
32 Contestant #3 Confident Judge #2
20 Contestant #3 Costume Judge #2
45 Contestant #3 Talent Judge #2

Confident = (sum of same criterias [2] ) / number of judges [2]
Costume = (sum of same criterias [2]) / number of judges [2]
Talent = (sum of same criterias [2]) / number of judges [2]
Total = Confident + Costume + Talent

FINAL OUTPUT
Contestant Name Confident Costume Talent Total
Contestant #1 29 13.5 40 82.5
Contestant #2
Contestant #3

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Multiple computation in array of data

Hi,

Is this data stored in Database? Yes, you can get all the data from table, and then sum all column elements:prevar arr = [1,2,3,5];
var sum = 0;
arr.forEach(function(element) {
sum+=element;
});/pre

Lonlon
Posts: 0
Joined: Sat Jun 14, 2014 7:04 am

Multiple computation in array of data

Hi Katya,

I got unexpected token error, please see my code below

var DB_id='53903445e4b0889b402e6c81',
collectionName='tblScore',
columnName = 'numScore',
competitionId = request.get("competition");

var params = {}; //Define parameters object
params.criteria = { //Query criteria:
"charCompetition": {"$inQuery" : {"charCompetition":competitionId}}
};

try {
query = Collection.query(DB_id, collectionName, params);

var arr = query;
var sum = 0;
arr.forEach(query(element) {
sum+=element;
}
}
response.success({totalSum: sum + "%"}, "application/json");
} catch (e) {
response.success({message: e.message, code: e.code}, "application/json");
}

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Multiple computation in array of data

Hello Lonlon ,

First, you need to use

codearr.forEach(function(element)/code

not

codearr.forEach(query(element)/code

And, secondly, you need to summarize some specific property from an entry, i.e.:

codearr.forEach(function(element) {
sum+=parseInt (element ["someProperty"]);
}/code

Return to “Issues”