hi, thanks for ppl answered my last thread and i did get that overcome. thanksss
big picture:
now. last last problem, my program generates 10 random questions with random wrong answers in radio and one correct answer also in radio. when i click checkAnswer it should check the right answer and compare to the user provided answer.
please check out isCorrect() function (and see comment) .. i tried diff things and couldn't get the value out from the radio checked value.
i spent 5 hours on this issue .. any help would be great.
<HTML>
<HEAD>
<TITLE>Experiments with Simple Objects.</TITLE>
<SCRIPT Language="JavaScript">
<!--
checkedResults = 0
checkSummary = 0
questions = new Array()
for (i = 0; i < 10; i++)
{
questions[i] = new Question(Math.floor(Math.random()*5) + 3, Math.floor(Math.random()*5) + 3, i)
}
function Question(n1, n2, slot)
{
this.slot = slot
this.tries = 0
this.correct = false
this.question = "What is the sum of " + n1 + " and " + n2 + "?"
this.answer = n1 + n2
this.handleGuess = handleGuess
this.writeQuestion = writeQuestion
this.isCorrect = isCorrect
}
function clearAndFocus(slot)
{
document.quiz['q'+slot].value = " "
document.quiz['q'+slot].focus()
}
//////////// this function is not doing the job /////////////
function isCorrect()
{
//////// how do i get the radio checked value ??? ////////////
if (document.quiz["q" + this.slot].value == this.answer)
{
this.correct = true
return true
}
else
{
this.correct = false
//clearAndFocus(this.slot)
return false
}
}
/**
* write the question into an already existing form.
* expects to be passed the number in the question array
* where this question is stored.
**/
function writeQuestion()
{
//Write out the question:
document.writeln("<P>" + this.question+"</P>")
document.writeln("<P>")
var right = this.answer+"<INPUT type=radio name=q"+this.slot+" value="+this.answer+">"
var wrong1 = (this.answer+1)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer+1)+ ">"
var wrong2 = (this.answer+2)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer+2)+ ">"
var wrong3 = (this.answer-3)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer-3)+ ">"
var wrong4 = (this.answer-2)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer-2)+ ">"
var zero = true
var one = true
var two = true
var three = true
var four = true
var r
while( (zero == true) || (one == true) || (two == true) || (three == true) || (four == true) )
{
r = Math.floor(Math.random()*5)
if(r == 0)
{
if(zero == true)
{
document.writeln(right)
zero = false
}
}
if(r ==1)
{
if(one ==true)
{
document.writeln(wrong1)
one= false
}
}
if(r ==2)
{
if(two == true)
{
document.writeln(wrong2)
}
two=false
}
if(r ==3)
{
if(three == true)
{
document.writeln(wrong3)
}
three=false
}
if(r ==4)
{
if(four == true)
{
document.writeln(wrong4)
}
four=false
}
}
document.writeln("</p>")
document.writeln("<P>")
//Write out the first part of the input button
document.writeln("<INPUT TYPE=BUTTON value=\"Check Your Answer\" ")
//Write out the onClick event handler for example if n is 1 onClick="questions.[1].handleGuess(this.form.q1.value)"
// document.writeln("onClick=\"questions[" + this.slot + "].handleGuess(getRadioValue(document.quiz.q"+this.slot+"))\">")
document.writeln("onClick=\"questions[" + this.slot + "].handleGuess(getRadioValue(this))\">")
//Write out the paragraph ending.
document.writeln("</P>\n")
}
function getRadioValue(radioArray)
{
var i
for(i=0; i<radioArray.length; i++)
{
if(radioArray[i].checked)
{
return radioArray[i].value
alert(radioArray[i].value)
}
return ""
}
}
function handleGuess(guess)
{
this.tries++
// alert(document.quiz.q0.getRadioValue(this))
if (guess == this.answer)
{
this.correct = true
checkResults++
alert("Correct!")
}
else
{
this.correct = false
alert("Not Correct.")
//clearAndFocus(this.slot)
}
}
function showResults()
{
checkSummary++
var tries = checkedResults
var correct = 0
var total = questions.length
for (i = 0; i < total; i++){
tries += questions[i].tries
correct += questions[i].isCorrect()
}
alert("You got " + correct + " questions right out of a total of " + total + "\n" +
"You checked your answer " + tries + " times."+" \nAnd you check the summary "+checkSummary+" times")
}
//-->
</SCRIPT>
</HEAD>
<BODY onLoad="document.quiz.reset()">
<H1>Simple Adding Quiz Page</H1>
<FORM Name="quiz">
<SCRIPT Language="JavaScript">
<!--
for (i = 0; i < questions.length; i++)
{
questions[i].writeQuestion()
}
//-->
</SCRIPT>
<P><INPUT TYPE="BUTTON" Value="How did I do?" onClick="showResults()">
</P>
</FORM>
</BODY>
</HTML>