View Full Version : Problems with Javascript Assignment

10-13-2008, 05:00 AM
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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<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>
<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 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 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()">

Arty Effem
10-13-2008, 05:24 AM
Hi, can someone please help point me in the right direction? Head for the Firefox Error console:

missing ( before condition
Line 25

Look very carefully.

Also, single-line comments start with //

10-13-2008, 06:06 AM
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"


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:

if (document.candselect.business[2].checked == true) points += parseInt(document.candselect.business[2].value);

Unfortunately neither seem to be working for me

Arty Effem
10-13-2008, 02:40 PM
Firefox keeps telling me
"Error: document.candselect.business[2] is undefined"
Open code executes as soon as the script block containing it is parsed.

10-13-2008, 04:29 PM
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]

10-13-2008, 05:03 PM
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.