473,651 Members | 2,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1384
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><ti tle>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.getEle mentById("quiz" );
my_form.onsubmi t = function() { return checkAnswer(); };
}

function Question()
{
var the_field = document.getEle mentById("answe rfield");
the_field.value ="";
var the_div = document.getEle mentById("quest ions");
the_div.innerHT ML="";
var text_node = document.create TextNode(the_qu estions[i]);
the_div.appendC hild(text_node) ;
}

function checkAnswer()
{
var the_field = document.getEle mentById("answe rfield");

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

the_field.focus ();
the_field.selec t();
return false;
}
</script></head>
<body onload="attachH andlers(); 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
2636
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
5747
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 three best choices. If they do, a message window pops up which says good choices or something and take them to next question. If they don't choose all of the best choices they get another message like try again but these are not the best choices...
4
1926
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
548
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 users or save any data. If the application crashes or user exits the program they should simply start again. Pretty basic. All interactions are stored in an array (not much going on to save to a
3
12346
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 several pages. At the end unless they completed the quiz it then pops up a message stating that they took to long to complete the quiz. I already have a timing function working but the issue is when you go to the next page it obviously stops running. I've...
4
7462
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 questions and answers to cycle through until the last question? Also, how can I give him the score after the last question. Thank you in advance. DAL. P.S. As a beginner, I figured I couldn't pass up the chance to learn something new, and to practice...
2
3426
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 throughout the quiz. And well how to display the result of the quiz and te grade of the person who has taken the quiz....
3
2212
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 Answers. - Each Answer is assigned a Weighted value.... for each type of Quiz Result. - Weighted values are in the range of -6, 0, +6. - Each Quiz will also apply the same Weight range for if a M/F is taking and what of 6 age groups the taker is...
0
2241
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. The problem I have is the first record gets processed fine where as the second record always; reason being the EAI expects it to be a seperate call Though I am creating/reseting a new service everytime within the foreach loop the data seems to be...
0
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.