Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a multiple choice quiz for speed test. Each question appears
after every five seconds and then the next, like a slide show. The
timer runs simultaneously. The last page that appears is score summary.
I can't figure out how to show the correct score though I have written
codes for it. The summary page shows the score 0 each time.
Could you please help me. Thanks a lot.


What I have tried:

<body>

        <div id="container">
        <text> Time : <text id="time001"> 30 </text> </text> <br />
        <h1> Speed Test </h1>
        <button id="btn" onclick="begin001()"> Start </button>
    
         <form id="form">
         <div class="speed">
         <h2> Q.1 Who is the Prime Minister of India? </h2>
         <label> <input type="radio" name="q1" value="a" id="q1a"> Atal 
         Bihari </label> <br>
         <label> <input type="radio" name="q1" value="b" id="q1b"> 
         Narendra Modi </label> <br>
         <label> <input type="radio" name="q1" value="c" id="q1c"> 
         Rahul Gandhi </label> <br>
         <label> <input type="radio" name="q1" value="d" id="q1d"> 
         Soniya Gandhi </label> <br>
         </div>
    
          <div id="summary">
           <p> Score Summary </p>
           </div>
            </form>
           </div>
    </body>
    
    ```

         $('document').ready(function() {
         $(function() {
         $("#btn").click(function() {
         var interval = setInterval(function() {
         $("#form > div:first")
         .fadeIn(500)
         .next()
         .fadeOut(500)
         .end()
         .appendTo("#form");
          }, 5000);


```
          window.setTimeout(function() {
          clearInterval(interval);
          }, 35000);
          });
          });
          });


  ```

         function begin001() {
            c = 30;
         }
          var a = 0;
          a++;
          var b = 0;
          b++;
    
    ```
          function timer001() {
          c = c - 1;
          if (c < 30)
    
            {
                time001.innerHTML = c;
            }
    
            if (c < 1)
    
            {
                window.clearInterval(update);
    
            }
    
          }
    
          update = setInterval("timer001()", 1000);
    
    
    ```
    
          var q1 = document.getElementById("form")["q1"].value;
          var q2 = document.getElementById("form")["q2"].value;
          var q3 = document.getElementById("form")["q3"].value;
          var q4 = document.getElementById("form")["q4"].value;
          var q5 = document.getElementById("form")["q5"].value;
    
    ```
    
          var answers = ["b", "c", "a", "d", "c"];
          var score = 0;
          var total = 5;
          if (q1 == answers[0]) {
            score++
          }
          if (q2 == answers[1]) {
            score++
          }
          if (q3 == answers[2]) {
            score++
          }
          if (q4 == answers[3]) {
            score++
          }
          if (q5 == answers[4]) {
            score++
          }
    
    ```
          var result = document.getElementById("summary");
          result.innerHTML = "<p><p> You scored "+score+" out of "+total+" </p> 
          </p>";
    
    ```
          Expected: You scored 2 out of 5.
    
          Currently showing: You scored 0 out of 5.'
Posted
Updated 21-Apr-19 8:23am
v2

That is a symptom of mixed quotes in the line that shows that message. Change the ' characters to "
 
Share this answer
 
Comments
Tarun Rathore 20-Apr-19 19:57pm    
Please a little elaboration.
Dave Kreskowiak 20-Apr-19 20:50pm    
You've got mismatched quotes. You started the string literal with a " character, then tried to put the score in the string with ' + score + '. The single quotes do not pair up with the double quote that you started the literal with. You cannot mix them!
result.innerHTML = "<p> You scored '+score+' out of '+total+ '</p>";

becomes:
result.innerHTML = "<p> You scored "+score+" out of "+total+ "</p>";
Tarun Rathore 21-Apr-19 14:09pm    
Thanks a lot for your help, but the problem still exists. Score 0 each time.
Dave Kreskowiak 21-Apr-19 19:54pm    
You modified the question and changed the problem description. What I told you was accurate to the previous version of the question.
Quote:
Expected: You scored 2 out of 5.
Currently showing: You scored 0 out of 5.

This step is debugging, replace the timer thing with a button and use the debugger to see what is going on inside your page.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900