...

View Full Version : Writing a multiple choice quiz



NevadaSam
06-04-2005, 03:00 AM
I want to write a javascript for a multiple choice quiz. If I can get the code for one question, the rest I may be able to do. I want to alert the user if they are wrong and give the correct answer. If the user get the question right I want the alert to say "you are correct". The last part is what I am having trouble with. This is what I have so far. Thanks for any hints you can give me.

<html><head><title>Multiple choice quiz</title>
<script language="JavaScript">
function quiz(question1){
if(question1.q1.value != "4"){
alert("The correct answer is: 4");
}
else{
if(question1.q1.value == "4");
alert("You are correct!");
}
}
</script>
</head>
<body>
<b>
<form name="question1"
method="post"
onSubmit="return quiz(document.question1)"><p>
<font size="+1"><p>
Question 1:<br><br>
2 plus 2 is:<br>
<input type=radio
name="q1"
value="45">a. 45<br>
<input type=radio
name="q1"
value="4">b. 4<br>
<input type=radio
name="q1"
value="3">c. 3<br>
<input type=radio
name="q1"
value="6">d. 6<br>
<p>
<input type=button
value="Click to Submit" onClick="quiz(this.form);">
<input type="reset" value="Clear">
</form>
</body>
</html>

devosc
06-04-2005, 04:48 AM
<html><head><title>Multiple choice quiz</title>
<script language="JavaScript">
function quiz(question1){
//alert(question1.q1[1].checked);
if(question1.q1[1].checked == false){
alert("The correct answer is: 4");
}
else{
if(question1.q1[1].checked == true);
alert("You are correct!");
}
}
</script>
</head>
<body>
<b>
<form name="question1"
method="post"
onSubmit="return quiz(document.question1)"><p>
<font size="+1"><p>
Question 1:<br><br>
2 plus 2 is:<br>
<input type=radio
name="q1"
value="45">a. 45<br>
<input type=radio
name="q1"
value="4">b. 4<br>
<input type=radio
name="q1"
value="3">c. 3<br>
<input type=radio
name="q1"
value="6">d. 6<br>
<p>
<input type=button
value="Click to Submit" onClick="quiz(this.form);">
<input type="reset" value="Clear">
</form>
</body>
</html>
Read the section on debugging, e.g. uncomment the above alert statement, FireFox seems to be more informative about the types of html/dom elements being accessed.

NevadaSam
06-05-2005, 07:40 PM
Thanks devosc,

That is what I was looking for.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum