Looping Issue w/ Basic Quiz...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LayneMitch via WebmasterKB.com

    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:

    Code:
    <html><head><title>Problem7</title>
    <script type="text/javascript">
    
    function attachHandlers()
    {
    var my_button = document.getElementById("sec_button");
    my_button.onclick = checkAnswer;
    }
    
    function firstQuestion()
    {
    var the_box = document.getElementById("questions");
    var first_ques = "what's the value of 5+4?";
    the_stat = document.createTextNode(first_ques);
    the_box.appendChild(the_stat);
    }
    
    function checkAnswer()
    {
    var the_questions = new Array();
    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";
    
    var the_field = document.getElementsByTagName("input")[0];
    
    var i=0;
    while(i < the_key.length)
    {
    
    if (the_field.value==the_key[i])
    {
    alert("Very Good");
    i++;
    }
    
    else
    {
    alert("Please try again");
    return
    }
    
    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);
    }
    
    }
    </script></head>
    <body onLoad="attachHandlers(); firstQuestion();">
    <div id = "questions"></div>
    <input type="text" id="answerfield" size="20"><br>
    <input type="button" value="check" id="sec_button">
    </body>
    </html>
    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


  • SAM

    #2
    Re: Looping Issue w/ Basic Quiz...

    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

    Comment

    • LayneMitch via WebmasterKB.com

      #3
      Re: Looping Issue w/ Basic Quiz...

      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

      Comment

      Working...