Unanswered question

How to made statistics (percentage table) ?

On input, we have a survey (web) as below:
Grp_name;Nb_Members ;Agree ;
Group1 ; 8 ;No ;
Group2 ; 2 ;Yes ;
Group3 ; 6 ;Yes ;
Group4 ; 4 ;Yes ;

On output, we would like to return the total and the percentage, by group and by members, for each answers.
________ ;Yes ;No ;
Nb_Groups ;3(75%) ;1(25%) ;
Nb_Members ;12(60%);8(40%) ;

Details:
On each test, the number of groups or members can change, even the answers.

I would like to know if there is a function which can help us ?
Or should I create a Javascript myself ?

Eric N.
Eric N.

Eric N.

Level
0
79 / 100
points

Answers

Eric N.
Eric N.

Eric N.

Level
0
79 / 100
points

So, I extracted the variables as below:
Grp_name=[Group1,Group2,Group3,Group4]
Nb_Members=[8,2,6,4]
Agree=[No,Yes,Yes,Yes]

Then we have to build the new variable like this:
URL={"survey":{"yes":{"Nb_Groups":3,"percentage1":75,"Nb_Members":12,"percentage2":60},
"no":{"Nb_Groups":1,"percentage1":25,"Nb_Members":8,"percentage2":40}}}

Eric N.
Eric N.

Eric N.

Level
0
79 / 100
points

Finally, I created a script as below. I hope it will help someone one day.

var text ="";
var i = 1;
var temp = 0;
var agree = "";
var TotalGrps = 0;
var TotalMembers = 0;
var votesGrpsYes = 0, votesGrpsNo = 0;
var votesMembsYesPer = 0, votesMembsNoPer = 0;
var votesYesPer = 0, votesNoPer = 0;
var nb_MembersYes = 0, nb_MembersNo = 0;

TotalGrps = context.variableManager.getValue("Grp_name_matchNr");
text += "\"survey\":[";

while ( i <= TotalGrps ) {
temp = context.variableManager.getValue("Nb_Members_"+i);
agree = context.variableManager.getValue("Agree_"+i);

if (agree == "yes") {
nb_MembersYes += parseInt(temp);
votesGrpsYes++;
} else {
nb_MembersNo += parseInt(temp);
votesGrpsNo++;
}
TotalMembers += parseInt(temp);
i++;
}

votesYesPer = (votesGrpsYes / TotalGrps) * 100;
votesNoPer = (votesGrpsNo / TotalGrps) * 100;

votesMembsYesPer = (nb_MembersYes / TotalMembers) * 100;
votesMembsNoPer = (nb_MembersNo / TotalMembers) * 100;

votesMembsYesPer = Math.round(votesMembsYesPer * 100) / 100;
votesMembsNoPer = Math.round(votesMembsNoPer * 100) / 100;

text += "{\"yes\":{\"Grps\":"+votesGrpsYes+",\"per\":"+votesYesPer+",\"GrpsMembers\":"+nb_MembersYes+",\"perMembers\":"+votesMembsYesPer+"},\"no\":{\"Grps\":"+votesGrpsNo+",\"per\":"+votesNoPer+",\"GrpsMembers\":"+nb_MembersNo+",\"perMembers\":"+votesMembsNoPer+"}}]";

context.variableManager.setValue("JavaScriptVariable_1",text);