Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Javascript to make graphs for elections - not working either

    Hello here is another one i need suggestions on

    here is my coding so far

    <html>
    <head>
    <!--
    New Perspectives on JavaScript
    Tutorial 3
    Case Problem 2

    Congressional Election Results
    Author:
    Date: November 14 2010

    Filename: election.htm
    Supporting files: back.jpg, logo.jpg, results.css, votes.js

    -->
    <title>Congressional Races</title>
    <link href="results.css" rel="stylesheet" type="text/css" />
    script type="text/javascript" src="votes.js"></script>

    <script type="text/javascript"> // script element to add the votes.js

    function totalVotes(votes){ // script element that will calculate the array
    var total = 0;
    for (var i = 0; i < votes.length; i++);
    {
    total = total + votes[i];
    }
    return total;
    }

    function calcPercent(item,sum){ // script element to calculate the percentage which is rounded to the nearest integer
    var item = Math.round((item/sum))*100); // script element to get the percentage
    return item;
    }
    calcPercent();

    function createBar(partyType,percent){ // script element to create blank cells
    switch(partyType) {
    case D: <td class='dem'></td>
    break;
    case R: <td class='rep'></td>
    break;
    case I: <td class='ind'></td>
    break;
    case G: <td class='green'></td>
    break;
    case L: <td class='lib'></td>
    break;
    }
    var barTxt = partyType;

    for (i=0; i < percent; i++){
    document.write(barText);
    }

    }
    function showResults(race,name,party,votes){ // script element to display the results of a particular race
    var totalIV = totalVotes(votes);
    document.write("<h2>race</h2>");
    document.write("<table cellspacing='0'>");
    document.write("<tr>");
    document.write("<th>Candidate</th>");
    document.write("<th class ='num'>Votes</th>");
    document.write("<th class='num'>%</th>");
    document.write("</tr>");

    for (var i=0; i < name.length; i++){
    document.write("<tr>");
    document.write("<td>name(party</td>");
    document.write("td class='num'>"votes</td>");

    var percent=calcPercent(votes[i], totalV)

    document.write("<td class='num'>(" + percent +"%)</td>");

    createBar(party[i],percent)
    document.write("</tr>");
    }
    document.write("</table>");
    }
    </script>

    </head>

    <body>

    <div id="intro">
    <p><img src="logo.jpg" alt="Election Day Results" /></p>
    <a href="#">Election Home Page</a>
    <a href="#">President</a>
    <a href="#">Senate Races</a>
    <a href="#">Congressional Races</a>
    <a href="#">State Senate</a>
    <a href="#">State House</a>
    <a href="#">Local Races</a>
    <a href="#">Judicial</a>
    <a href="#">Referendums</a>
    </div>



    <div id="results">
    <h1>Congressional Races</h1>
    <script type="text/javascript">
    showResults(race[0],name1,party1,votes1)...
    showResults(race[1],name2,party2,votes2)...
    showResults(race[2],name3,party3,votes3)...
    showResults(race[3],name4,party4,votes4)...
    showResults(race[4],name5,party5,votes5)...
    </script>

    </div>

    </body>
    </html>

    ______________

    What i need to do is make a bar charts of elections people on the assignment, using 2 other files on a folder
    the names of the other files
    are votes.js and results.css

    here is the link of the actual question
    it is case problem 2
    if u scroll down

    at

    http://books.google.ca/books?id=aG_T...Cell()&f=false




    right now i did up my coding and still nothing is working

    what does seem to be wrong with my coding?

    any suggestions from anyone? or hints even?
    thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,029
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    showResults(race[0],name1,party1,votes1)...

    Perhaps others. Have you used your error console?

    When posting here please follow the posting guidelines and wrap your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.


    It is your responsibility to die() if necessary….. - PHP Manual

  • Users who have thanked Philip M for this post:

    worldtraveller (11-15-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts

  • Users who have thanked Sciliano for this post:

    worldtraveller (11-15-2010)

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Location
    canada
    Posts
    131
    Thanks
    51
    Thanked 0 Times in 0 Posts
    I use textwrangler for my MAC computer. for doing javascript on.
    is there any others text editor programs that work better? thanks


  •  

    Tags for this Thread

    Posting Permissions

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