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>The problem goes with the slot argument, resulting to 0 value no matter where the count starts.
I'll provide you with some brief example later...
Here's a simple demo of getting the correct the value of the supplied question, and then alerts the user if he/she got the right answer associated in its field.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html id="html40L" lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Window-target" content="_top">
<title>Free Live Help!</title>
<style type="text/css">
<!--
-->
</style>
<script type="text/javascript">
<!--
var Question = function( counter ) {
this.counter = counter;
};
Question.prototype = {
tries : 0,
question : function( n1, n2 ) {
return {
questions : n1 + " and " + n2 };
},
getQ : function() {
for ( var i = 0; i < this.counter; i++ ) {
return this.question(( Math.floor( Math.random() * 5 ) + 3 ), ( Math.floor( Math.random() * 5 ) + 3 ));
}
},
createInput : function( lab, val, ids ) {
return " " + lab + " <input type=\"radio\" value=\"" + (( val ) ? val : "" ) + "\"" + " id=\"" + (( ids ) ? ids : "" ) + "\">";
},
start : function() {
var n = 1;
var m = 5;
var tip;
var cue;
var ans;
var total = "";
var input = [ ];
tip = this.getQ().questions;
cue = String( tip ).match(/\d+/g);
ans = parseInt((( cue[ 0 ] * 1 ) + cue[ 1 ] * 1 ));
var question = "";
total = this.createInput( ans, ans, "q" + ans );
num = ( 4 );
input.push( total );
while( num >= n ) {
input.push( this.createInput((( num ) * (( num === m ) ? m -= 1 : m -= 1 ))));
input.sort( function() { return .5- Math.random(); } );
num--; }
question += "<p><b>Q:</b> What is the sum of " + cue[ 0 ] + " and " + cue[ 1 ] + "?";
question += "<label for=\"" + (( "q" ) + ans ) + "\"><b>A:</b></label> " + input.join(" ") + "";
question += "<button onclick=\"(( document.getElementById ) ? document.getElementById('q' + " + ans + ") : document.all['q' + " + ans + "] ).checked ? alert('Correct Answer') : alert('Wrong Answer');\">Check Answer</button>";
return {
question : question,
input : input,
answer : ans }
}
};
var $QandA = new Question( 10 );
window.onload = function() {
for ( var j = 0; j < $QandA.counter; j++ ) {
document.writeln( $QandA.start().question + "</p><hr>" );
}
};
// -->
</script>
</head>
<body>
<div id="output"></div>
</body>
</html>