Reply
Stuck! Please help.
Old 08-22-2007, 12:17 AM Stuck! Please help.
Novice Talker

Posts: 12
Location: Somewhere on a Planet.
So what I have to do is below and the code I got is below that. I have no idea what I am supposed to be doing. Think my brain has died. I am supposed to be testing the following ballot in it:
01132310331013322132332221330330212003102332032010 13201120030202030221202020232122211330303122021100 3

Implement a program that determines the winner in a first-past-the post election. The names of the candidates and their candidate numbers are given in Table 1 below. Your program only needs to work with these four candidates. It does not need to be designed to work with elections in general, just this particular one. You can assume that the voting record only includes the characters 0, 1, 2 and 3. Use the file task03 as your starting point. Complete the function countVotes() so that it determines the winner, making use of the functions getNextVote() and getRemainingVotes(). Your finished program should:
obtain the total number of votes for each candidate;
display the total number of votes awarded to each candidate;
display the name of the winning candidate;
display the total number of votes cast.

Candidate Candidate number
Andy 0
Barbara 1
Charles 2
Daphne 3

// 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
//
// GET THE VOTING RECORD FROM THE TEXT BOX IN THE HTML PAGE
//
someVotes = document.votingForm.votingRecord.value;

//
// CALCULATE THE RESULTS OF THE ELECTION HERE
//

//
// 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.close(); // CLOSE THE PAGE AFTER WRITING THE RESULTS
}

Any help is very much appreciated.


Last edited by NinjaKitty : 08-22-2007 at 12:19 AM.
NinjaKitty is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 08-22-2007, 01:07 AM Re: Stuck! Please help.
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
If this is homework, as I suspect it is (no real-world application would be this poorly designed and useless, nor would it use Javascript), I'm not going to give you the full answer. But I will tell you this:

getNextVote will give you the value (0, 1, 2, 3) of the leftmost number in that string full of garbage you posted here:

01132310331013322132332221330330212003102332032010 13201120030202030221202020232122211330303122021100 3

getRemainingVotes will remove the first vote from the string and spit out all the remaining ones at the time.

So the rough idea that you want is:

get the next vote
assign it to a variable corresponding with whoever was voted for
get the remaining votes
repeat 1-3 until the allVotes string is down to 1 vote (you won't be able to get the remaining votes because your substring will be 0 in length).

Print out the results
Show which candidate won
Count number of votes

That should get you started.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 08-22-2007, 01:16 AM Re: Stuck! Please help.
Novice Talker

Posts: 12
Location: Somewhere on a Planet.
Well I am still confused. But that probably means I am just stupid.

Thanks for the info. (I don't think it is really called homework when you are 26.)
NinjaKitty is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Stuck! Please help.
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14451 seconds with 12 queries