473,376 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

Looping Issue w/ Basic Quiz...

Hello.

This is a basic quiz. Only about 3 questions. I'm having issue with the
answer key which stays on a certain value regardless of acknowledging that
you have the correct answer for the first and moving on to the next answer.

Here's the code:

Expand|Select|Wrap|Line Numbers
  1. <html><head><title>Problem7</title>
  2. <script type="text/javascript">
  3.  
  4. function attachHandlers()
  5. {
  6. var my_button = document.getElementById("sec_button");
  7. my_button.onclick = checkAnswer;
  8. }
  9.  
  10. function firstQuestion()
  11. {
  12. var the_box = document.getElementById("questions");
  13. var first_ques = "what's the value of 5+4?";
  14. the_stat = document.createTextNode(first_ques);
  15. the_box.appendChild(the_stat);
  16. }
  17.  
  18. function checkAnswer()
  19. {
  20. var the_questions = new Array();
  21. the_questions[1] = "what's the value of 7+4?";
  22. the_questions[2] = "what's the value of 9+6?";
  23. the_questions[3] = "what's the value of 10+6?";
  24.  
  25.  
  26. var the_key = new Array();
  27. the_key[0] = "9";
  28. the_key[1] = "11";
  29. the_key[2] = "15";
  30. the_key[3] = "16";
  31.  
  32. var the_field = document.getElementsByTagName("input")[0];
  33.  
  34. var i=0;
  35. while(i < the_key.length)
  36. {
  37.  
  38. if (the_field.value==the_key[i])
  39. {
  40. alert("Very Good");
  41. i++;
  42. }
  43.  
  44. else
  45. {
  46. alert("Please try again");
  47. return
  48. }
  49.  
  50. the_field.value="";
  51. var the_div = document.getElementById("questions");
  52. the_div.innerHTML="";
  53. var text_node = document.createTextNode(the_questions[i]);
  54. the_div.appendChild(text_node);
  55. }
  56.  
  57. }
  58. </script></head>
  59. <body onLoad="attachHandlers(); firstQuestion();">
  60. <div id = "questions"></div>
  61. <input type="text" id="answerfield" size="20"><br>
  62. <input type="button" value="check" id="sec_button">
  63. </body>
  64. </html>
  65.  
The answer key is staying on one value then checking it without me hitting
the button....

I know it's probably a simple step missing, but I can't seem to find it.

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200808/1

Aug 27 '08 #1
2 1364
SAM
LayneMitch via WebmasterKB.com a écrit :
>
Here's the code:
(snip) (see previous post)
The answer key is staying on one value then checking it without me hitting
the button....
yes the 'while' continues its job
I know it's probably a simple step missing, but I can't seem to find it.
without a reference (globale one) I don't see how to do.

There is how I'ld do :

<html><head><title>Problem7</title>
<script type="text/javascript">

var i=0;
var the_questions = new Array();
the_questions[0] = "what's the value of 5+4?";
the_questions[1] = "what's the value of 7+4?";
the_questions[2] = "what's the value of 9+6?";
the_questions[3] = "what's the value of 10+6?";

var the_key = new Array();
the_key[0] = "9";
the_key[1] = "11";
the_key[2] = "15";
the_key[3] = "16";

function attachHandlers()
{
var my_form = document.getElementById("quiz");
my_form.onsubmit = function() { return checkAnswer(); };
}

function Question()
{
var the_field = document.getElementById("answerfield");
the_field.value="";
var the_div = document.getElementById("questions");
the_div.innerHTML="";
var text_node = document.createTextNode(the_questions[i]);
the_div.appendChild(text_node);
}

function checkAnswer()
{
var the_field = document.getElementById("answerfield");

if (the_field.value==the_key[i])
{
alert("Very Good");
i++;
if( i<the_key.length ) Question();
}
else
{
alert("Please try again");
}

the_field.focus();
the_field.select();
return false;
}
</script></head>
<body onload="attachHandlers(); Question();">
<p id = "questions"></p>
<form id="quiz" action="#" >
<input type="text" id="answerfield" size="20"><br>
hit key [Enter] or this button: <input type="submit" value="check">
</form></body></html>

--
sm
Aug 27 '08 #2
SAM wrote:
>LayneMitch via WebmasterKB.com a écrit :
Appreciate the response...the problem is solved. Thanks once again.

--
Message posted via http://www.webmasterkb.com

Aug 28 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sketcher | last post by:
Hi, I am trying to create a quiz, Code is as follows: <html> <head> <title>Quiz</title> </head> <BODY> <Center><TABLE cellSpacing=3 cellPadding=0 border=0>
5
by: Vandana Rola | last post by:
Hello Everyone, I am a beginner in Javascript.I want to create fun quiz tool using javascript (no database). The feature of that tool is every question has five choices. The user needs to select...
4
by: Ryan Ternier | last post by:
I'm having an issue with an inner loop. Here's the basic Code: For each......{ //Do code here //start other loop For each....{
5
by: Sean | last post by:
Problem with sessions I have created an application without concern for sessions. As it turns out I think that might be my undoing. What I have: I have an online quiz. I don’t need to know...
3
by: nawhaley | last post by:
Ok I'm working on a timer that will work over multiple pages. Basically what this means is I need the timer to start on page one and give the user x amount of time to complete a quiz that spans...
4
by: DAL | last post by:
I want to build my kid a program that cycles through questions (using a label for the question), and lets him choose one of two radio buttons for the right answer. How do I get every set of...
2
by: kenny | last post by:
I'm making a quiz to be posted on the internet. but I'm facing difficulties in finding a simple timer for the quiz (unlimited timing) which will keep on running regardless to the change of the page...
3
by: empiresolutions | last post by:
I am building a app that creates quizzes. This is how it goes - - Create Quiz - Provide up to 10 different types of Quiz Results - Give up to 50 Questions - Each Question has up to 10 possible...
0
by: vigneshrao | last post by:
Hi, I have been working on a script that loops through multiple records and sends data (one record per call) to a WS. I am supposed to make a new call for each record before sending the data....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.