SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member featch1's Avatar
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation student programmer can u check my code plse

    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>
    Last edited by featch1; Sep 6, 2007 at 04:41. Reason: no replies

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,253
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)

    comments

    Hi featch1, welcome to the forums,
    When I start a new project, I write the steps as comments in outline form. eg.
    /* database connection code here */
    /* get GET vars and sanitize */
    /* assign GET var vals to db vars */
    /* query string here */
    /* perform db stuff */
    /* HTML stuff here */
    then I fill in the code step by step.
    I often have better luck if I spend some time thinking about what steps are needed to do what I want to do first, before jumping right into the code first.
    Nothing personal, but it seems that although you do have an idea of what you want the code to do, you are missing some crucial steps in the code logic.

  3. #3
    SitePoint Member featch1's Avatar
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Hi featch1, welcome to the forums,
    When I start a new project, I write the steps as comments in outline form. eg.
    /* database connection code here */
    /* get GET vars and sanitize */
    /* assign GET var vals to db vars */
    /* query string here */
    /* perform db stuff */
    /* HTML stuff here */
    then I fill in the code step by step.
    I often have better luck if I spend some time thinking about what steps are needed to do what I want to do first, before jumping right into the code first.
    Nothing personal, but it seems that although you do have an idea of what you want the code to do, you are missing some crucial steps in the code logic.
    Hi Mittineaque, thanks for the advice as its an assignment I have to code it in the format given - the countVotes() is where I'm getting stuck but still playing with it - I'm now looking at two for loops but need to do something with the candidate array.

    <HTML>
    <HEAD>

    <TITLE>A first-past-the-post vote counting program</TITLE>
    <SCRIPT language = "JavaScript">
    // REPLACEMENT FILE
    //
    // A FUNCTION TO EXTRACT ONE OR MORE VOTES FROM THE FRONT OF //VOTING RECORD

    function getNextVote(allTheVotes, lengthOfBallot)
    {
    var aVote = '';
    aVote = allTheVotes.substr(0, lengthOfBallot);
    return (aVote);
    }

    // A FUNCTION TO REMOVE ONE OR MORE VOTES FROM THE FRONT OF A
    //VOTING RECORD ONCE THEY HAVE BEEN COUNTED BY ANOTHER 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
    //
    var count;
    var candidateName =['Andy','Barbara','Charles','Daphne'];
    var theVote =['0','1','2','3'];
    var totalVote = someVotes.length

    for (var i=0; i < someVotes.length; i=i+1)
    {

    for (var j=0; j < theVote.length; j=j+1)
    {

    if (someVotes.charAt(i) == theVote[j])
    {
    document.write(someVotes.charAt(i) + ' ');
    count = count + 1;
    }
    };


    // 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('Total number of votes cast was ' + totalVote + ' <BR>');
    document.write('<BR>'+'No of votes for candidateName[0] is ' +
    count);

    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>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •