Connecting Tech Pros Worldwide Forums | Help | Site Map

I have an error. I am brain dead. Please give me a hint

Newbie
 
Join Date: Nov 2006
Posts: 9
#1: Nov 16 '06
What is the best checker to use to find these probles with, besides an experienced set of eyes?

This probram is to grade the response to input by user. Grades his marks.
Thanks
[HTML]
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Math Quiz</title>

<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function recordAnswer(question, answer) {
if (question = 1)
document.forms[0].studentAnswer1.value = answer;
else if (question = 2)
document.forms[0].studentAnswer2.value = answer;
else if (question = 3)
document.forms[0].studentAnswer3.value = answer;
}
var tooLong = false;
function scoreQuiz(){
if (tooLong == true)
alert("You ran out of time! You must retake the test by reloading the Web page.");
}
else if
{
if (document.forms[0].studentAnswer1.value == "" || document.forms[0].studentAnswer2.value == "" || document.forms[0].studentAnswer3.value == "") {
alert(You must answer all of the questions before scoring the quiz.);
return false;
}
var correct = 0;
if (document.forms[0].studentAnswer1.value == document.forms[0].answer1.value)
++correct;
if (document.forms[0].studentAnswer2.value == document.forms[0].answer2.value)
++correct;
if (document.forms[0].studentAnswer3.value == document.forms[0].answer3.value)
++correct;
alert("You scored " + correct + " answers correctly.");
}
return true;

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>
<body onload="setInterval('tooLong=true;', 600000);">
<h1>Math Quiz</h1>
<p>Select the correct answer to the following math problems, then
select the Score Quiz button.</p>

<form action="" method="get" onsubmit="return scoreQuiz();">

<p><b>1.</b>&nbsp; 84 divided by 7 is equal to _____.</p>

<p><input type="radio" name="question1" value="6" onclick=
"recordAnswer(1, this.value);" />6<br />
<input type="radio" name="question1" value="7" onclick=
"recordAnswer(1, this.value);" />7<br />
<input type="radio" name="question1" value="9" onclick=
"recordAnswer(1, this.value);" />9<br />
<input type="radio" name="question1" value="12" onclick=
"recordAnswer(1, this.value);" />12</p>

<p><b>2.</b>&nbsp; What is the value of x in the equation <b>x * 12
= 156</b>.</p>

<p><input type="radio" name="question2" value="5" onclick=
"recordAnswer(2, this.value);" />5<br />
<input type="radio" name="question2" value="11" onclick=
"recordAnswer(2, this.value);" />11<br />
<input type="radio" name="question2" value="13" onclick=
"recordAnswer(2, this.value);" />13<br />
<input type="radio" name="question2" value="19" onclick=
"recordAnswer(2, this.value);" />19</p>

<p><b>3.</b>&nbsp; What is the square root of 196?</p>

<p><input type="radio" name="question3" value="7" onclick=
"recordAnswer(3, this.value);" />7<br />
<input type="radio" name="question3" value="14" onclick=
"recordAnswer(3, this.value);" />14<br />
<input type="radio" name="question3" value="28" onclick=
"recordAnswer(3, this.value);" />28<br />
<input type="radio" name="question3" value="98" onclick=
"recordAnswer(3, this.value);" />98</p>

<p><input type="submit" value=" Score Quiz" /></p>

<p><input type="hidden" name="answer1" value="12" /> <input type=
"hidden" name="answer2" value="13" /> <input type="hidden" name=
"answer3" value="14" /> <input type="hidden" name="studentAnswer1"
value="" /> <input type="hidden" name="studentAnswer2" value="" />
<input type="hidden" name="studentAnswer3" value="" /></p>
</form>
<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
</p>
</body>
</html>
[/HTML]

Edited by iam_clint: reason: Added html tags around your code do it for yourself from now on please. read posting guidelines.

Expert
 
Join Date: Oct 2006
Location: NC
Posts: 1,722
#2: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


What do you mean by checker? A code validator? That will only make sure you have correct syntax. What problem are you having with this code?
Newbie
 
Join Date: Nov 2006
Posts: 9
#3: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


Quote:

Originally Posted by AricC

What do you mean by checker? A code validator? That will only make sure you have correct syntax. What problem are you having with this code?

Syntax problem. Something to do with "else if"
I thought there was something out there that would give me hints of what was wrong. Using word pad
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#4: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


Sloppy code let me fix it up.
Newbie
 
Join Date: Nov 2006
Posts: 9
#5: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


Thank you and thank for the direction to posting guide lines.
Expert
 
Join Date: Oct 2006
Location: NC
Posts: 1,722
#6: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


I use the Visual Studio Web Developer Express Edition it's free I can't remember how/if it validates javascript, but it isn't too shabby for a free product.


Aric
Newbie
 
Join Date: Nov 2006
Posts: 9
#7: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


I got it. I will have to learn it now. Thanks
just downloaded it at your suggestion
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,076
#8: Nov 16 '06

re: I have an error. I am brain dead. Please give me a hint


[HTML]
<html>
<title>Math Quiz</title>
<script>
var tooLong = false;

function recordAnswer(question, answer) {
if (question == 1) {
document.getElementById("studentAnswer1").value = answer;
}
if (question == 2) {
document.getElementById("studentAnswer2").value = answer;
}
if (question == 3) {
document.getElementById("studentAnswer3").value = answer;
}
}

function test() {
alert("test");
}

function scoreQuiz() {
allanswers = true;
if (document.getElementById("studentAnswer1").value == "" || document.getElementById("studentAnswer3").value == "" || document.getElementById("studentAnswer2").value == "") {
allanswers = false;
}
if (tooLong == true) {
alert("You ran out of time! You must retake the test by reloading the Web page.");
return false;
}
var correct = 0;
if (allanswers) {
if (document.getElementById("studentAnswer1").value == document.getElementById("answer1").value) {
++correct;
}
if (document.getElementById("studentAnswer2").value == document.getElementById("answer2").value) {
++correct;
}
if (document.getElementById("studentAnswer3").value == document.getElementById("answer3").value) {
++correct;
}
if (correct == 3) {
alert("You got all 3 correct!");
} else {
alert("You scored "+correct+" answers correctly.");
}
return true;
} else {
alert("You have to answer all questions!");
}
}

</script>
<body onload="setInterval('tooLong=true;', 600000);">

<h1>Math Quiz</h1>
<p>Select the correct answer to the following math problems, then
select the Score Quiz button.</p>

<p><b>1.</b>&nbsp; 84 divided by 7 is equal to _____.</p>

<p>
<input type="radio" name="question1" value="6" onclick="recordAnswer(1, this.value);">6<br>
<input type="radio" name="question1" value="7" onclick="recordAnswer(1, this.value);">7<br>
<input type="radio" name="question1" value="9" onclick="recordAnswer(1, this.value);">9<br>
<input type="radio" name="question1" value="12" onclick="recordAnswer(1, this.value);">12</p>
<p><b>2.</b>&nbsp; What is the value of x in the equation <b>x * 12
= 156</b>.</p>

<p><input type="radio" name="question2" value="5" onclick="recordAnswer(2, this.value);" />5<br />
<input type="radio" name="question2" value="11" onclick="recordAnswer(2, this.value);" />11<br />
<input type="radio" name="question2" value="13" onclick="recordAnswer(2, this.value);" />13<br />
<input type="radio" name="question2" value="19" onclick="recordAnswer(2, this.value);" />19</p>

<p><b>3.</b>&nbsp; What is the square root of 196?</p>

<p><input type="radio" name="question3" value="7" onclick="recordAnswer(3, this.value);" />7<br />
<input type="radio" name="question3" value="14" onclick="recordAnswer(3, this.value);" />14<br />
<input type="radio" name="question3" value="28" onclick="recordAnswer(3, this.value);" />28<br />
<input type="radio" name="question3" value="98" onclick="recordAnswer(3, this.value);" />98</p>

<p><input type="button" value=" Score Quiz" onclick="scoreQuiz();" /></p>

<p><input type="hidden" name="answer1" id="answer1" value="12" /> <input type="hidden" name="answer2" id="answer2" value="13" /> <input type="hidden" name="answer3" id="answer3" value="14" /> <input type="hidden" name="studentAnswer1" id="studentAnswer1" value="" /> <input type="hidden" name="studentAnswer2" id="studentAnswer2" value="" />
<input type="hidden" name="studentAnswer3" id="studentAnswer3" value="" /></p>
</body>
</html>
[/HTML]
Heres your fix I tried to not modify the code too much so you could see what is happening.
Reply