when i run the following program i get a window.prompt for number of candidates, then window.pront for candidates names, then the prompts for number of votes, once this is done everyhing displys fine on the webpage, except the candidates names, each one just says undefined. Where am am i going wrong ? Thankyou
* Program to summarise the results of the election for Secretary.
There were 5 candidates.
*/
// Declare variables
var totalMembership, numberOfCandidates, totalVote;
var candidateNameArray = ['Bloggs', 'Bennett', 'Dehesh', 'Kelly', 'Turner'];
var candidateVoteArray = [0, 0, 0, 0, 0];
var numberofCandidates = 5;
var nameArray = new Array(5);
numberofCandidates = 5;
numberofCandidates = window.prompt('enter number of candidates','')
numberofCandidates = parseFloat(numberofCandidates);
candidateNameArray = window.prompt('enter name of first candidate','')
candidateNameArray = window.prompt('enter name of second candidate','')
candidateNameArray = window.prompt('enter name of third candidate','')
candidateNameArray = window.prompt('enter name of fourth candidate','')
candidateNameArray = window.prompt('enter name of fifth candidate','')
candidateNameArray = parseFloat(candidateNameArray);
var candidateNameArray = new Array(5);
// For each candidate the number of votes cast is entered
// and the running total of votes cast so far is calculated
numberOfCandidates = candidateNameArray.length;
totalVote = 0;
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
candidateVoteArray[count] = candidateVoteArray[count] +
parseFloat(window.prompt
('Please enter the votes cast for candidate ' + (count + 1)));
totalVote = totalVote + candidateVoteArray[count];
}
// The total votes cast is written out
document.write('Total votes cast: ' + totalVote + '<BR>');
/*For each candidate, their total vote and their percentage share of the vote is output*/
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
var percentageVote;
percentageVote = (candidateVoteArray[count] / totalVote) * 100;
percentageVote = Math.round(percentageVote * 100) / 100;
document.write(candidateNameArray[count] +
'........votes: ' + candidateVoteArray[count] +
'........% of total vote: ' + percentageVote + '<BR>');
}
/* The winning candidate is declared by determining the array index with the largest vote
(assumes that there is an overall winner and no possibility of a tied ballot) */
var maxIndex;
maxIndex = 0;
for (var count = 0; count < candidateNameArray.length; count = count + 1)
if (candidateVoteArray[count] > candidateVoteArray[maxIndex])
{
maxIndex = count;
}
document.write('Winner is Candidate ' + (maxIndex+1) + ' with '
+ candidateVoteArray[maxIndex] + ' votes');
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Software/Hardware used:
ASKED:
June 16, 2009 9:40 AM
UPDATED:
June 26, 2009 11:18 AM