943,692 Members | Top Members by Rank

Ad:
Jun 19th, 2009
0

what is wrong with the check?

Expand Post »
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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Experiments with Simple Objects.</TITLE>
  5.  
  6. <SCRIPT Language="JavaScript">
  7. <!--
  8. checkedResults = 0
  9. checkSummary = 0
  10. questions = new Array()
  11.  
  12.  
  13. for (i = 0; i < 10; i++)
  14. {
  15. questions[i] = new Question(Math.floor(Math.random()*5) + 3, Math.floor(Math.random()*5) + 3, i)
  16. }
  17.  
  18.  
  19. function Question(n1, n2, slot)
  20. {
  21. this.slot = slot
  22. this.tries = 0
  23. this.correct = false
  24. this.question = "What is the sum of " + n1 + " and " + n2 + "?"
  25. this.answer = n1 + n2
  26. this.handleGuess = handleGuess
  27. this.writeQuestion = writeQuestion
  28. this.isCorrect = isCorrect
  29. }
  30.  
  31. function clearAndFocus(slot)
  32. {
  33. document.quiz['q'+slot].value = " "
  34. document.quiz['q'+slot].focus()
  35. }
  36.  
  37. //////////// this function is not doing the job /////////////
  38. function isCorrect()
  39. {
  40. //////// how do i get the radio checked value ??? ////////////
  41. if (document.quiz["q" + this.slot].value == this.answer)
  42. {
  43. this.correct = true
  44. return true
  45. }
  46. else
  47. {
  48. this.correct = false
  49. //clearAndFocus(this.slot)
  50. return false
  51. }
  52. }
  53.  
  54. /**
  55.  * write the question into an already existing form.
  56.  * expects to be passed the number in the question array
  57.  * where this question is stored.
  58.  **/
  59. function writeQuestion()
  60. {
  61. //Write out the question:
  62. document.writeln("<P>" + this.question+"</P>")
  63. document.writeln("<P>")
  64.  
  65. var right = this.answer+"<INPUT type=radio name=q"+this.slot+" value="+this.answer+">"
  66. var wrong1 = (this.answer+1)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer+1)+ ">"
  67. var wrong2 = (this.answer+2)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer+2)+ ">"
  68. var wrong3 = (this.answer-3)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer-3)+ ">"
  69. var wrong4 = (this.answer-2)+"<INPUT type=radio name=q" + this.slot + " value="+(this.answer-2)+ ">"
  70.  
  71.  
  72. var zero = true
  73. var one = true
  74. var two = true
  75. var three = true
  76. var four = true
  77. var r
  78.  
  79. while( (zero == true) || (one == true) || (two == true) || (three == true) || (four == true) )
  80. {
  81. r = Math.floor(Math.random()*5)
  82.  
  83. if(r == 0)
  84. {
  85. if(zero == true)
  86. {
  87. document.writeln(right)
  88. zero = false
  89. }
  90. }
  91.  
  92. if(r ==1)
  93. {
  94. if(one ==true)
  95. {
  96. document.writeln(wrong1)
  97. one= false
  98. }
  99. }
  100.  
  101. if(r ==2)
  102. {
  103. if(two == true)
  104. {
  105. document.writeln(wrong2)
  106. }
  107. two=false
  108. }
  109.  
  110. if(r ==3)
  111. {
  112. if(three == true)
  113. {
  114. document.writeln(wrong3)
  115. }
  116. three=false
  117. }
  118.  
  119. if(r ==4)
  120. {
  121. if(four == true)
  122. {
  123. document.writeln(wrong4)
  124. }
  125. four=false
  126. }
  127. }
  128.  
  129.  
  130. document.writeln("</p>")
  131.  
  132. document.writeln("<P>")
  133. //Write out the first part of the input button
  134. document.writeln("<INPUT TYPE=BUTTON value=\"Check Your Answer\" ")
  135. //Write out the onClick event handler for example if n is 1 onClick="questions.[1].handleGuess(this.form.q1.value)"
  136. // document.writeln("onClick=\"questions[" + this.slot + "].handleGuess(getRadioValue(document.quiz.q"+this.slot+"))\">")
  137. document.writeln("onClick=\"questions[" + this.slot + "].handleGuess(getRadioValue(this))\">")
  138. //Write out the paragraph ending.
  139.  
  140. document.writeln("</P>\n")
  141. }
  142.  
  143. function getRadioValue(radioArray)
  144. {
  145. var i
  146.  
  147. for(i=0; i<radioArray.length; i++)
  148. {
  149. if(radioArray[i].checked)
  150. {
  151. return radioArray[i].value
  152. alert(radioArray[i].value)
  153.  
  154. }
  155.  
  156. return ""
  157. }
  158. }
  159.  
  160.  
  161.  
  162.  
  163. function handleGuess(guess)
  164. {
  165. this.tries++
  166. // alert(document.quiz.q0.getRadioValue(this))
  167. if (guess == this.answer)
  168. {
  169. this.correct = true
  170. checkResults++
  171. alert("Correct!")
  172. }
  173. else
  174. {
  175. this.correct = false
  176. alert("Not Correct.")
  177. //clearAndFocus(this.slot)
  178. }
  179. }
  180.  
  181.  
  182.  
  183.  
  184. function showResults()
  185. {
  186. checkSummary++
  187. var tries = checkedResults
  188. var correct = 0
  189. var total = questions.length
  190.  
  191. for (i = 0; i < total; i++){
  192. tries += questions[i].tries
  193. correct += questions[i].isCorrect()
  194. }
  195. alert("You got " + correct + " questions right out of a total of " + total + "\n" +
  196. "You checked your answer " + tries + " times."+" \nAnd you check the summary "+checkSummary+" times")
  197. }
  198.  
  199.  
  200. //-->
  201. </SCRIPT>
  202. </HEAD>
  203.  
  204. <BODY onLoad="document.quiz.reset()">
  205. <H1>Simple Adding Quiz Page</H1>
  206. <FORM Name="quiz">
  207. <SCRIPT Language="JavaScript">
  208. <!--
  209. for (i = 0; i < questions.length; i++)
  210. {
  211. questions[i].writeQuestion()
  212.  
  213. }
  214.  
  215.  
  216. //-->
  217. </SCRIPT>
  218.  
  219. <P><INPUT TYPE="BUTTON" Value="How did I do?" onClick="showResults()">
  220. </P>
  221. </FORM>
  222. </BODY>
  223.  
  224. </HTML>
Similar Threads
k2k
Reputation Points: 15
Solved Threads: 1
Posting Whiz
k2k is offline Offline
351 posts
since Nov 2007
Jun 19th, 2009
0

Re: what is wrong with the check?

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...
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Jun 20th, 2009
0

Re: what is wrong with the check?

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.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html id="html40L" lang="en">
  4. <head>
  5. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <meta http-equiv="Content-Style-Type" content="text/css">
  8. <meta http-equiv="Content-Script-Type" content="text/javascript">
  9. <meta http-equiv="Window-target" content="_top">
  10. <title>Free Live Help!</title>
  11. <style type="text/css">
  12. <!--
  13.  
  14. -->
  15. </style>
  16. <script type="text/javascript">
  17. <!--
  18.  
  19. var Question = function( counter ) {
  20. this.counter = counter;
  21. };
  22.  
  23. Question.prototype = {
  24. tries : 0,
  25. question : function( n1, n2 ) {
  26. return {
  27. questions : n1 + " and " + n2 };
  28. },
  29. getQ : function() {
  30. for ( var i = 0; i < this.counter; i++ ) {
  31. return this.question(( Math.floor( Math.random() * 5 ) + 3 ), ( Math.floor( Math.random() * 5 ) + 3 ));
  32. }
  33. },
  34. createInput : function( lab, val, ids ) {
  35. return " " + lab + " <input type=\"radio\" value=\"" + (( val ) ? val : "" ) + "\"" + " id=\"" + (( ids ) ? ids : "" ) + "\">";
  36. },
  37.  
  38. start : function() {
  39. var n = 1;
  40. var m = 5;
  41. var tip;
  42. var cue;
  43. var ans;
  44. var total = "";
  45. var input = [ ];
  46. tip = this.getQ().questions;
  47. cue = String( tip ).match(/\d+/g);
  48. ans = parseInt((( cue[ 0 ] * 1 ) + cue[ 1 ] * 1 ));
  49. var question = "";
  50. total = this.createInput( ans, ans, "q" + ans );
  51. num = ( 4 );
  52. input.push( total );
  53. while( num >= n ) {
  54. input.push( this.createInput((( num ) * (( num === m ) ? m -= 1 : m -= 1 ))));
  55. input.sort( function() { return .5- Math.random(); } );
  56. num--; }
  57. question += "<p><b>Q:</b> What is the sum of " + cue[ 0 ] + " and " + cue[ 1 ] + "?<br><br>";
  58. question += "<label for=\"" + (( "q" ) + ans ) + "\"><b>A:</b></label> " + input.join(" ") + "<br>";
  59. question += "<button onclick=\"(( document.getElementById ) ? document.getElementById('q' + " + ans + ") : document.all['q' + " + ans + "] ).checked ? alert('Correct Answer') : alert('Wrong Answer');\">Check Answer</button>";
  60. return {
  61. question : question,
  62. input : input,
  63. answer : ans }
  64. }
  65. };
  66.  
  67. var $QandA = new Question( 10 );
  68.  
  69. window.onload = function() {
  70. for ( var j = 0; j < $QandA.counter; j++ ) {
  71. document.writeln( $QandA.start().question + "</p><hr>" );
  72. }
  73. };
  74. // -->
  75. </script>
  76. </head>
  77. <body>
  78. <div id="output"></div>
  79. </body>
  80. </html>
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Jquery & JscrollPane - ScrollBy function in javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Footer table fixing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC