Calculate percentage in Multi-Dimensional Arrays
In my Javascript Multi-Dimensional Arrays, it stores 3 values: NAME, ROUND, No. of Vote
Sample data:
NAME| ROUND| No. of Vote
Evan | 1 | 2
John | 1 | 1
Mary | 1 | 3
Evan | 2 | 4
John | 2 | 1
I want to display the Voting result on screen with percentage (based on ROUND), Just Like that:
ROUND 1
Evan - 2 Votes (33.3%)
John - 1 Vote (16.7%)
Mary - 3 Votes (50%)
ROUND 2
Evan - 4 Votes (80%)
John - 1 Vote (20%)
Thanks, Joe
The following is my javascript code:.
function rpt_result(){
var temp = '';
var l_temp = '';
var html = '';
var num = obj('remain').value;
var total = 0;
var tmp_round = 0;
html += '<html>';
html += '<head><title>Voting Result</title>';
html += '</head>';
html += '<body>';
html += '<table width="760" align="center"><tr><td align="center"><h2>' + obj('ta_title').value + '</h2></td></tr>';
html += '<tr><td> </td></tr>';
html += '<tr><td align="center">Each Round Results</td>';
html += '</table>';
html += '<div id="button" style="width:760px;margin-top:20px;margin-bottom:20px;"><input type="button" value="Print" onmouseup="window.print()" style="float:right;" /></div>';
html += '<table width="760" align="center" border="0">';
html += '<tr>'
html += '<td><b><u>Winner:</b></u></td>';
html += '</tr>';
html += '<tr><td>';
for (i = 0; i < winnerArray.length; i++) {
temp = winnerArray[i].split('|', 3);
vote = temp[0];
name = temp[1];
round = temp[2];
html += '(The' + num2Chi(round) + 'Round) ' + name + ' ' + vote + 'Votes' + '<br />';
}
html += '</td></tr>';
html += '<tr><td> </td></tr>';
html += '<tr>'
html += '<td><b><u>Detail Voting Result:</b></u></td>';
html += '</tr>';
html += '<tr><td>';
for (i = 0; i < wholeArray.length; i++) {
w_temp = wholeArray[i].split('|', 4);
w_vote = w_temp[0];
w_name = w_temp[1];
w_round = w_temp[2];
html += '<font color="red">(The' + num2Chi(w_round) + 'Round) ' + w_name + ' ' + w_vote + ' Votes </font><br />';
}
html += '</td></tr>';
html += '</table>';
html += '</body>';
html += '</html>';
var x = window.open('', 'printList', "location=0,menubar=0,scrollbars=1,status=0,toolbar=0,resizable=1,开发者_运维问答width=800,height=600");
x.document.write(html);
x.document.close();
}
You need 2 loops, one to count ALL the votes, and set it into a sum variable, and the other the calculate the percentage of each.
精彩评论