/    Sign up×
Community /Pin to ProfileBookmark

Displaying names and numbers newbie

First past the post election program I want this progrram to:

  • 1. Obtain the number of votes for each candidate

  • 2. Display the number of votes awarded to each candidate

  • 3. 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>

    to post a comment
    JavaScript

    0Be the first to comment 😎

    ×

    Success!

    Help @JemHadar spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...