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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Problems with Javascript Assignment

    Hi, can someone please help point me in the right direction? This is for my web programming class. I almost had this done hours ago, everything seemed to be working and I went to try and add 1 more thing and it has been a downward spiral since then. Now I can't even get it back to the way it was. I am new to javascript so please dumb it down as much as possible. I am so frustrated right now I can't see straight. If someone can tell me where my problem is I would really appreciate it. I want to do it myself but my textbook is horrible.

    This is the website
    http://rcoloyan.myweb.usf.edu/eme6930/asst2.html

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title>EME 6930 Web Programming 1 Assignment 2</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    <script language="JavaScript" type="text/javascript"><!-- 	
    
    function welcome()
    {	
    alert('Welcome to the 2008 Presidential Candidate Selector'); 
    }	
    function selector()
    {	
    var points = 0;	\\total of points assigned to questions
    if (document.candselect.business[2].checked == true) points += 2;	
    if (document.candselect.business[1].checked == true) points += 1;	
    if (document.candselect.iraq[2].checked == true) points += 2;
    if (document.candselect.iraq[1].checked == true) points += 1;
    if (document.candselect.oil[2].checked == true) points += 2;
    if (document.candselect.oil[1].checked == true) points += 1;
    if (document.candselect.healthcare[2].checked == true) points += 2;
    if (document.candselect.healthcare[1].checked == true) points += 1;
    if {document.candselect.gun[2].checked == true) points += 2;
    if (document.candselect.gun[1].checked == true) points += 1;
    
    var candidate = (points >= 6) ? "Barack Obama" : "John McCain";   \\candidate selected based on points
    var firstname = "document.candselect.fname"  \\name of user
    alert(firstname + 'Based upon these five issues the candidate you should vote for is ' + candidate);
    } 
    
    // --></script>
    </head>
    <body onLoad="welcome()">
    <h1 id="center">2008 Presidential Election Candidate Selector</h1>
    <div id="left">
    <h2>John McCain</h2><h2><img src="media/mccain.jpg" alt="John McCain" height="250" width="200"></h2>
    <p.text><a href="http://www.johnmccain.com/">John McCain's Campaign Website</a></p>
    </div>
    <div id="left"><h2>Barack Obama</h2><h2><img src="media/obama.jpg" alt="Barack Obama"></h2>
    <p.text><a href="http://www.barackobama.com/index.php/">Barack Obama's Campaign Website</a></p>
    </div>
    <div id="content">
    <h2>Please complete the following political survey to find the best candidate for you</h2>
    <form name="candselect">Please enter your first name
    <input type="text" name="fname" maxlength="40" size="40"><br /><br /><br /><br />Are you in favor of lower taxes for business?<br />
    <input type="radio" name="business" value="1">Yes
    <input type="radio" name="business" value="2">No<br /><br />Are you in favor of a quick withdrawal of all troops from Iraq?<br />
    <input type="radio" name="iraq" value="2">Yes
    <input type="radio" name="iraq" value="1">No<br /><br />Are you in favor of more oil drilling<br />
    <input type="radio" name="oil" value="1">Yes
    <input type="radio" name="oil" value="2">no<br /><br />Are you in favor of universal heathcare?<br />
    <input type="radio" name="healthcare" value="2">Yes
    <input type="radio" name="healthcare" value="1">No<br /><br />Are you in favor of gun control?<br />
    <input type="radio" name="gun" value="2">Yes
    <input type="radio" name="gun" value="1">No<br /><br />
    <input type="reset" value="clear answers"><br /><br />
    <input type="button" name="btnselect" value="who should you vote for?" onClick="selector()">
    </form>
    </div>
    </body>
    </html>
    Last edited by pheenix11; 10-13-2008 at 04:05 AM.

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by pheenix11 View Post
    Hi, can someone please help point me in the right direction?
    Head for the Firefox Error console:

    missing ( before condition
    http://rcoloyan.myweb.usf.edu/eme6930/asst2.html
    Line 25

    Look very carefully.

    Also, single-line comments start with //
    Last edited by Arty Effem; 10-13-2008 at 04:27 AM.

  • Users who have thanked Arty Effem for this post:

    pheenix11 (10-13-2008)

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ahh I see, I had a { instead of a (

    Ok I fixed that and the comments problem. Firefox keeps telling me
    "Error: document.candselect.business[2] is undefined"

    Is

    Code:
    if (document.candselect.business[2].checked == true) points += 2;
    The right way to do this? I want it to add 2 points to the points variable if true. I saw some examples for my class where they did something like the following:

    Code:
    if (document.candselect.business[2].checked == true) points += parseInt(document.candselect.business[2].value);
    Unfortunately neither seem to be working for me
    Last edited by pheenix11; 10-13-2008 at 05:09 AM.

  • #4
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by pheenix11 View Post
    Firefox keeps telling me
    "Error: document.candselect.business[2] is undefined"
    Open code executes as soon as the script block containing it is parsed.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Code:
    if (document.candselect.business[2].checked == true) points += 2;	
    if (document.candselect.business[1].checked == true) points += 1;
    in javascript arrays and collections start counting their elements always from 0, not from 1, thus you must use in your code [0] instead of [1] and [1] instead of [2]
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    pheenix11 (10-13-2008)

  • #6
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much, that was it! I started the numbering at 0 and was able to add the other things I wanted as well in a matter of minutes.

    Thank you again! This has been driving me crazy.

    Rich


  •  

    Posting Permissions

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