Hello all on the forum, I hope you can help as my tutor doesn't seem contactable at present, I have an assignment which I don't expect you to do for me but feel I'm going around in circles - and looking for someone to point me in the right direction. It is a voting system that gets a vote, then gets remaining vote and calculates how many each candidate was voted for from the record I have i tried the charAt method to separate the individual votes but I'm sure there must be a better way.
01132310331013322132332221330330212003102332032010132011200302
020302212020202321222113303031220211003
Posted all my code below that I have done so far any advice if their is a better way or something I have missed.
Many thanks in advance
featch
<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
//
function countVotes ()
{
var someVotes = ''; // THE COMPLETE VOTING RECORD FROM THE MACHINE
var theVote = ''; // A SINGLE VOTE FROM THE VOTING RECORD
var candidateName = new Array ('Andy','Barbara','Charles','Daphne');
var candidateNumber = new Array (0,1,2,3);
candidateNumber[0] = 0;
candidateNumber[1] = 1;
candidateNumber[2] = 2;
candidateNumber[3] = 3;
var vcount = 0;
//
// GET THE VOTING RECORD FROM THE TEXT BOX IN THE HTML PAGE
//
someVotes = document.votingForm.votingRecord.value;
//
// CALCULATE THE RESULTS OF THE ELECTION HERE
//
var totalVote = someVotes.length;
for (var count = 0; count <= someVotes.length; count++)
{
if (someVotes.charAt(count) == '0' || '1' || '2' || '3')
vcount = vcount + 1;
document.write('Count is ' + count + '<BR>') // TO SHOW LOOP WORKING THROUGH STRING
document.write(someVotes.charAt(count) + ' ')// SHOWS STRING ITERATED
}
//
// PRINT OUT THE RESULTS HERE
//
document.open(); // PREPARE THE HTML PAGE FOR WRITING
document.write('<P>The results of the election are as follows\/P>');
document.write('The number of votes cast for '+ candidateName[0] + ' in the string is ' + candidateNumber[0] + '<BR>');
document.write('The number of votes cast for '+ candidateName[1] + ' in the string is ' + candidateNumber[1] + '<BR>');
document.write('The number of votes cast for '+ candidateName[2] + ' in the string is ' + candidateNumber[2] + '<BR>');
document.write('The number of votes cast for '+ candidateName[3] + ' in the string is ' + candidateNumber[3] + '<BR>');
document.write('The number of votes cast for all candidates in the string is ' + totalVote + '<BR>');
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>


\/P>');





Bookmarks