I've only recently started javascript and I'm a java programmer so I'm sure I've done something obvious that I can't see. Basically this goes with an html document which has a number of questions on it, when the form is submitted it gets run through the getScore method. The questions are named q1, q2 etc.. If anyone with experience could help me it would be much appreciated.
Thanks, Dan
Thanks, Dan
Code:
var a = new Array(form.q1.value, form.q2.value, form.q3.value, form.q4.value, form.q5.value);
var rightAnswers = new Array(3,4,"ul",1,2);
var score = 0;
function getScore(form) {
if(a[0] == null || a[1] == null || a[2] == "" || a[2] == null || a[3] == null || a[4] == null){
alert("You haven't answered all of the questions yet.");
} else {
for(var i = 0; i<5 ; i++){
if(a[i] == rightAnswers[i]) score++;
}
var percent = score*20;
alert("You scored "+score". That's "+percent+"%.");
}
}
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>The Big Quiz - 885278</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="Ex5_885278.js"></script>
</head>
<body>
<h1>The Big Quiz</h1>
<form action="" method="post">
<h2>Question 1</h2>
<p>Which of these code snippets is valid?</p>
<ul>
<li><input type="radio" name="q1" value="1" /><a href=index.html>to there</li>
<li><input type="radio" name="q1" value="2" /><a href="index.html">to there</li>
<li><input type="radio" name="q1" value="3" /><a href="index.html">to there</a></li>
<li><input type="radio" name="q1" value="4" /><a href="index.html">to there<a></li>
</ul>
<h2>Question 2</h2>
<p>Which of these colours is the most blue?</p>
<p>
<select name="q2">
<option value="1">#330000</option>
<option value="2">#006633</option>
<option value="3">#FFCCCC</option>
<option value="4">#0000FF</option>
</select>
</p>
<h2>Question 3</h2>
<p>What is the element name for an unordered list?</p>
<p>
<input type="text" name="q3"></input>
</p>
<h2>Question 4</h2>
<p>Which javascript event is used for clicking events?</p>
<p>
<select name="q4">
<option value="1">onclick</option>
<option value="2">onhover</option>
<option value="3">onpunch</option>
<option value="4">onactivate</option>
</select>
</p>
<h2>Question 5</h2>
<p>What is the name of the organisation that develops web standards?</p>
<ul>
<li><input type="radio" name="q5" value="1" />DOM</li>
<li><input type="radio" name="q5" value="2" />W3C</li>
<li><input type="radio" name="q5" value="3" />Microsoft</li>
<li><input type="radio" name="q5" value="4" />Google</li>
</ul>
<p>
<input type="submit" value="Submit" onclick="getScore(this.form)" />
</p>
</form>
</body>
</html>
Comment