First past the post election program I want this progrram to:
1. Obtain the number of votes for each candidate2. Display the number of votes awarded to each candidate3. Display the total number of votes cast
I’ve highlighted the bits I’m having problems with I’m a beginner to Javascript and its a bit of a struggle at the moment. I have done as much of the code as I can however, I just need some advice. Thanks in advance.
<HTML>
<HEAD>
<TITLE>A first-past-the-post vote counting program</TITLE>
<SCRIPT language = “JavaScript”>
// REPLACEMENT FILE
//
// ***** YOU DO NOT NEED TO MODIFY THIS FUNCTION **
// A FUNCTION TO EXTRACT ONE OR MORE VOTES FROM THE FRONT OF A VOTING RECORD
// *** YOU DO NOT NEED TO MODIFY THIS FUNCTION
//
function getNextVote(allTheVotes, lengthOfBallot)
{
var aVote = ”;
aVote = allTheVotes.substr(0, lengthOfBallot);
return (aVote);
}
//
// ***** YOU DO NOT NEED TO MODIFY THIS FUNCTION *****
// A FUNCTION TO REMOVE ONE OR MORE VOTES FROM THE FRONT OF A VOTING
// RECORD ONCE THEY HAVE BEEN COUNTED BY ANOTHER FUNCTION.
// ***** YOU DO NOT NEED TO MODIFY THIS FUNCTION *****
//
function getRemainingVotes(allTheVotes, lengthOfBallot)
{
allTheVotes = allTheVotes.substr(lengthOfBallot, allTheVotes.length – lengthOfBallot);
return (allTheVotes);
}
//
// THIS PROGRAM TAKES THE CONTENTS OF THE VOTING RECORD AND SENDS
// THEM TO THE TWO FUNCTIONS getNextVote() AND getRemainingVotes()
// FOR PROCESSING AFTER ALL VOTES HAVE BEEN CANCELLED IT DISPLAYS
// THE RESULTS OF THE ELECTION
//
[COLOR=”Red”][B]var someVotes = 0123;
function countVotes ()
{
// THE COMPLETE VOTING RECORD FROM THE MACHINE
var theVote = ”; // A SINGLE VOTE FROM THE VOTING RECORD
var count1 = 4;
var nameArray = [Andy’,’Barbara’,’Daphney’,’Sarah]
//
// GET THE VOTING RECORD FROM THE TEXT BOX IN THE HTML PAGE
//
theVote = document.votingForm.votingRecord.value;
window.alert(‘The current vote is ‘ + theVote); [/B][/COLOR]
//
// CALCULATE THE RESULTS OF THE ELECTION HERE
//
// NOW GET THE REMAINING LIST OF VOTES
//
// PRINT OUT THE RESULTS HERE
//
//document.open(); // PREPARE THE HTML PAGE FOR WRITING
[COLOR=”Red”][B]for(var count = 0;count < count1; count + 1)
{
someVotes = someVotes-theVote;
document.write(‘The results of the election are as follows:’ + someVotes);
}[/B][/COLOR]
//document.close(); // CLOSE THE PAGE AFTER WRITING THE RESULTS
}
</SCRIPT>
</HEAD>
<!–
YOU DO NOT NEED TO MODIFY, OR EVEN UNDERSTAND, ANY OF THE HTML BELOW THIS POINT
–>
<BODY>
<form action=”” method=”get” name=”votingForm”>
<input name=”votingRecord” type=”text” size=”40″ maxlength=”255″>
<input name=”countButton” type=”button” value=”Count the votes” onClick=”javascript:countVotes();”>
</form>
</BODY>
</HTML>