Can anyone spot an obvious error? Why is this Javascript generated table (showResults function) not showing up in my html? This always happens when I have a lot of literal values to quote...I'd appreciate any other comments on my formatting (white-space) as well!
<script type="text/javascript">
//calculates sum of all values within an array
function totalVotes(votes){ /
var total=0;
for (i=0;i<votes.length;i++){
total = votes[i] + total;
}
return total;
}
//calculates a percentage, rounded to nearest integer
function calcPercent(item, sum){
return Math.round((item/sum)*100);
}
//tests value of partyType parameter
function createBar(partyType, percent){
switch (partyType){
case D: barText="<td class='dem'></td>";
break;
case R: barText="<td class='rep'></td>";
break;
case I: barText="<td class='ind'></td>";
break;
case G: barText="<td class='green'></td>";
break;
case L: barText="<td class='lib'></td>";
break;
}
for(i=1;i<=percent;i++){
document.write(barText);
}
}
function showResults(race,name,party,votes){
var totalV=function totalVotes(votes);
document.write("<h2>" +race+ "</h2>");
document.write("<table cellspacing='0'>");
document.write("<tr>");
document.write("<th>Candidate</th>");
document.write("<th class='num'> Votes</th>");
document.write("<th class='num'>%</th>");
document.write("</tr>");
for (r=0;r<name.length;r++){
document.write("<tr>");
document.write("<td>"+name[i]+ '(' +party[r]+ ")" +"</td>");
document.write("<td class='num'>" +votes[r]+ "</td>");
var percent=function calPercent(votes[r],totalV)
document.write("<td class='num'>(" +percent[r]+ "%)</td>");
createBar(party[r], percent);
document.write("</tr>");
}
document.write("</table>");
}
var totalV=function totalVotes(votes);is not valid Javascript. I think you meanvar totalV=totalVotes(votes);.for(i=1;i<=percent;i++)as going back through your code heredocument.write("<td class='num'>(" +percent[r]+ "%)</td>");, I think you meant to writefor(i=1;i<percent.length;i++). One other point with your for-loops, it is better to usevarwhen declaring the variable you iterate over so it is disposed after the loop.