Page 1 of 1

Multiple computation in array of data

Posted: Fri Jun 20, 2014 4:04 pm
by Lonlon

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


Multiple computation in array of data

Posted: Fri Jun 20, 2014 5:39 pm
by Kateryna Grynko

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


Multiple computation in array of data

Posted: Sat Jun 21, 2014 3:11 am
by Lonlon

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");
}


Multiple computation in array of data

Posted: Mon Jun 23, 2014 6:45 pm
by Evgene Karachevtsev

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