0
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>A Simple Quiz</title>
    <link rel="stylesheet" href="styles.css">
    <script>
      var q = ["How many days are in an year?", "How many days does february have in a leap year?","How many hours equals to 1 day?"];
      var a = [365, 29, 24];
      var fd =["Incorrect! Its 365", "Incorrect! It has 29 days", "Incorrect! It has 24 hours"];

      function scores() {
        document.getElementById('output').innerHTML = askquestion();
      }

      function askquestion() {
        var score=0;
          for(var i=0; i<3; i++) {
            var question=prompt(q[i]);
            if (question==a[i]){
              alert("Correct Answer");
              score=score +1;
            }
            else {
              alert(fd[i]);
            }
          }
        return score
      }
    </script>
  </head>

  <body>
    <h1> A Simple Quiz </h2>
    <hr>
    <script> scores()</script>
    <h2> The score is: </h2> <output id="finalscore"></output>
    <hr>
  </body>
</html>

Above is my code. I'm trying to print the score next to "the score is: ". But I can't figure out a way! I know we can't use print statement or anything. But I was hoping there is a way out. Thanks in advance!

3
  • Do you want to change this score time to time?? or it will be render only on page load. Commented Oct 1, 2015 at 5:43
  • there are only 3 questions that appear through dialogue box, so once the function's (for loop) is over the score is finalized. Hope I told you what you wanted to know. Commented Oct 1, 2015 at 5:47
  • jsfiddle.net/hogfcjmc take a look. I have created a fiddle for you. Commented Oct 1, 2015 at 5:49

2 Answers 2

2

HTML:

<div id="output"></div>

Also, on a sidenote you have mistakely closed the <h1> tag as </h2> :)

Your JavaSript:

var q = ["How many days are in an year?", "How many days does february have in a leap year?","How many hours equals to 1 day?"];
var a = [365, 29, 24];
var fd =["Incorrect! Its 365", "Incorrect! It has 29 days", "Incorrect! It has 24 hours"];



function askquestion() {
  var score=0;
  for(var i=0; i<3; i++){
     var question=prompt(q[i]);
       if (question==a[i]){
        alert("Correct Answer");
        score=score +1;

       }
      else{
        alert(fd[i]);
       }
  }
document.getElementById('output').innerHTML = "Score is "+score; // On completion of the loop, you can append the result to your string this way.
}

askquestion();

Fiddle: http://jsfiddle.net/hogfcjmc/

Edit:

You have made 2 simple mistakes:-

1. When your score() method runs, the HTML still isn't present in the DOM. So any value set to innerHTML wouldn't work.

Solution:: Move your <script>scores()</script> after the HTML code like this.

.....
<h2> The score is: </h2> <output id="finalscore"></output>
<script> scores()</script>
....

2. Observe that you are using a wrong Id name output. It should be finalscore.

So your code will look like this:

document.getElementById('finalscore').innerHTML = askquestion();

Updated and working fiddle: http://jsfiddle.net/xhs10xve/

Sign up to request clarification or add additional context in comments.

4 Comments

You have made a trivial mistake...document.getElementById('finalscore').innerHTML it should be? and not document.getElementById('output').innerHTML
Also, when your script in the body runs, your HTML is not yet in the DOM..so the result isnt appended.
Hey! I knew how to get the score within the script itself. But I wanted it in the body tag. Could you please take a look at the area in the body tag where i provided space for the score? thats where i want the result to appear! thanks!
With those 2 changes, your code will work just fine. Let me know.
0

change the following line

document.getElementById('output').innerHTML = askquestion();

to

document.getElementById('finalscore').innerHTML = askquestion();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.