Web Analytics Made Easy -
StatCounter quiz solutions not graded - CodingForum

Announcement

Collapse
No announcement yet.

quiz solutions not graded

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • quiz solutions not graded

    I've got this automated quiz working, but now the user's answers are not accurately graded according to the solutions. Can someone tell me what is wrong with my code from the attached js file and the html below?

    <Head>
    .
    .
    .
    <script src="scripts/quiz1.js"></script>
    <script>
    /*Functions to score quiz.*/
    function Engine(question, answer) {
    yourAns[question]=answer;
    }
    </script>
    </HEAD>

    <BODY>
    .
    .
    .
    <!--Quiz Questions-->
    <form name="myquiz">
    <script>
    for (i=1; i<question.length; i++) {
    document.write (question[i] + "<br />");
    document.write ("<input type='radio' value='a' name='choiceA' onClick='Engine(i, this.value)'> ");
    document.write (choiceA[i] + "<br />");
    document.write ("<input type='radio' value='b' name='choiceB' onClick='Engine(i, this.value)'> ");
    document.write (choiceB[i] + "<br />");
    document.write ("<input type='radio' value='c' name='choiceC' onClick='Engine(i, this.value)'> ");
    document.write (choiceC[i] + "<br />");
    document.write ("<input type='radio' value='d' name='choiceD' onClick='Engine(i, this.value)'> ");
    document.write (choiceD[i] + "<br /><br />");
    }
    </script>
    <input type=button onClick="score()" value="Well... How did I do?">
    </form>
    </BODY>
    Attached Files
    Last edited by Seawalker0903; Feb 25, 2004, 12:31 PM.

  • #2
    document.write ("<input type='radio' value='a' name='choiceA' onClick='Engine(i, this.value)'> ");

    should be this,I think:


    document.write ("<input type='radio' value='a' name='choiceA' onClick='Engine("+i+", this.value)'> ");

    same goes for the othe lines

    Comment


    • #3
      and remember that array is zero-based, so you should start your loop at zero.

      for (i=0; i<question.length; i++) {
      Glenn
      vBulletin Mods That Rock!

      Comment


      • #4
        Thanks Garadon, your suggestion worked, to get the score to increment, but check out the quiz when on the website, it's still working funny...
        www.seawalkersource.com

        glenngv--my array is not zero-based

        Comment


        • #5
          Its acting funny because you grouped all Choice A's together..

          Try this, group all the choices for one question to be belonging to that question

          for (i=1; i<question.length; i++) {
          document.write (question[i] + "<br />");
          document.write ("<input type='radio' value='a' name=Question"+i+" onClick='Engine("+i+", this.value)'> ");
          document.write (choiceA[i] + "<br />");
          document.write ("<input type='radio' value='b' name=Question"+i+" onClick='Engine("+i+", this.value)'> ");
          document.write (choiceB[i] + "<br />");
          document.write ("<input type='radio' value='c' name=Question"+i+" onClick='Engine("+i+", this.value)'> ");
          document.write (choiceC[i] + "<br />");
          document.write ("<input type='radio' value='d' name=Question"+i+" onClick='Engine("+i+", this.value)'> ");
          document.write (choiceD[i] + "<br /><br />");
          }


          Edit: corrected the name attribute.
          Last edited by Unit; Feb 27, 2004, 01:58 PM.
          Nobody is Perfect. I am Nobody.

          Comment


          • #6
            Originally posted by Seawalker0903
            glenngv--my array is not zero-based
            Javascript arrays are always zero-based. If you didn't put any value to the first element, question[0], it is undefined and rendered useless.
            Glenn
            vBulletin Mods That Rock!

            Comment


            • #7
              oops bad semantics... I just didn't put anything into question[0], and hence it is undefined when I start at 0, and shows up as an undefined question in IE6 where I do my initial testing. Is that something unique IE6?

              Comment


              • #8
                I am a bit confused now...

                var a = new Array();
                a[1]="a";
                a[2]="3";
                a[5]="n";

                for (i in a)
                {
                alert("a["+i+"] ="+a[i] );
                }

                The above script implies that any array is treated as an associative array.. (tried it in IE, is it the same in all browsers?). Doesn't it mean that there is no starting index for a javascript array? If it were really a zero-based array, then it should show "a[0]=undefined"...

                The property of an associative array is that only the indices you specify when you add array elements are existant and anything else is not there.

                If we compare this with arrays in languages like C, memory is allocated for the 0th element even though you may not use it.. is it the same case in javascript?

                Any clarification is greatly appreciated! Thanks

                Still trying to learn the ropes of javascript
                Nobody is Perfect. I am Nobody.

                Comment


                • #9
                  Even wierder problem

                  So I made the change suggested by Unit...now it doesn't even register the radio button for two of the questions. What is wrong here?

                  As for the arrays...I'm going to say that they are associative also, since the example I'm using shows undefined for a[0] when printed to the html page.

                  Comment


                  • #10
                    I made a err.. mistake

                    i edited my post to reflect the correction.. can you try it?
                    Nobody is Perfect. I am Nobody.

                    Comment


                    • #11
                      Mistaken?!

                      Unit what mistake did you make? I can't find your change...

                      Comment


                      • #12
                        Originally posted by Unit
                        I am a bit confused now...


                        The above script implies that any array is treated as an associative array.. (tried it in IE, is it the same in all browsers?). Doesn't it mean that there is no starting index for a javascript array? If it were really a zero-based array, then it should show "a[0]=undefined"...

                        The property of an associative array is that only the indices you specify when you add array elements are existant and anything else is not there.

                        If we compare this with arrays in languages like C, memory is allocated for the 0th element even though you may not use it.. is it the same case in javascript?

                        Any clarification is greatly appreciated! Thanks
                        var a = new Array();
                        a[1]="a";
                        a[2]="3";
                        a[5]="n";

                        for (i in a)
                        {
                        alert("a["+i+"] ="+a[i] );
                        }

                        Still trying to learn the ropes of javascript
                        It is because you use for...in loop not the normal for loop. Try this:

                        var a = new Array();
                        a[1]="a";
                        a[2]="3";
                        a[5]="n";

                        for (var i=0;i<a.length;i++)
                        {
                        alert("a["+i+"] ="+a[i] );
                        }
                        Glenn
                        vBulletin Mods That Rock!

                        Comment

                        Working...
                        X