[B]Hi, I was wondering if someone would be so kind as to help me complete a program I am writing.
The program is designed to allow a user to input the number of candidates electing for secretary, then the names of candidates, then the votes for each candidate.
I think I have most code correct, but the output is showing undefined!
I have been working on this program for 2 days (approx 18hrs) now and am getting very stressed with it, hence turned to some friendly help!?
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT
language=”JavaScript”
type=”text/javascript”>
/* Program to summarise the results of the election for Secretary.
There were 5 candidates.
// Declare variables
var totalMembership, numberOfCandidates, totalVote; candidateName; candidateVote;
var candidateNameArray = [];
var candidateVoteArray = [];
var candidateName = new Array(candidateNameArray);
var candidateVote = new Array(candidateVoteArray);
totalVote = 0;
// For each candidate the number of votes cast is entered
// and the running total of votes cast so far is calculated
numberOfCandidates = parseFloat(window.prompt(‘How many candidates do you have? ‘, ”));
numberOfCandidate = new Array(candidateNameArray);
for (var count = 0; count < numberOfCandidates; count = count + 1)
{
numberOfCandidates[count] = numberOfCandidates[count] +
parseFloat(window.prompt(‘Please enter candidates name ‘ + (count + 1)));
}
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
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>
[B]Thank you for looking,
Regards
Jon.