Number of questions counter not showing up in prompt

Hi Guys, any idea why the numberOfquestions counter in the for-loop statement doesn’t show up?

Here is my code: http://hastebin.com/oqejecezax.xml

Thanks in advance for the help :smile:

It works for me, but there’s quite a bit you could improve upon with your code.

Here’s something to point you in the right direction:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Quiz</title>
  </head>
  <body>

    <div id="result"></div>

    <script>
      var correct = 0;

      var questions = [
        {
          "question": "Name a programming language that's also a gem",
          "answer": "RUBY"
        },
        {
          "question": "Name a programming language that's also a snake",
          "answer": "PYTHON"
        },
        {
          "question": "What language do you use to style web pages?",
          "answer": "CSS"
        },
        {
          "question": "What language do you use to build the structure of web pages?",
          "answer": "HTML"
        },
        {
          "question": "What language do you use to add interactivity to a web page?",
          "answer": "JAVASCRIPT"
        }
      ];

      var results = {
        "5": "gold",
        "4": "silver",
        "3": "bronze"
      };

      var resultDiv = document.getElementById("result");

      function displayResults(){
        resultDiv.innerHTML = "<p>You got " + correct + " out of 5 questions correct.<p>";

        if (correct < 3){
          resultDiv.innerHTML += "<p><strong>No crown for you. You need to study.</strong></p>";
        } else {
          resultDiv.innerHTML += "<p><strong>You earned a " + results[correct] + " crown.</strong></p>";
        }
      }

      function getQuestionsRemaining(i){
        return " [" + (questions.length - i) + " questions remaining]";
      }

      questions.forEach(function(obj, index){
        var answer = prompt(obj.question + getQuestionsRemaining(index)) || "";
        if (answer.toUpperCase() === obj.answer){
          correct += 1;
        }
      });

      displayResults();
    </script>
  </body>
</html>

Also, pls post your code here, not in pastebin.

I’ll leave it to felgall to mention that using prompt() might not be the best way to obtain input from your users.

In some browsers prompts look like this:

check the checkbox in the prompt (which was re-purposed for debugging after Netscape 4 died) and you kill JavaScript on that web page.

As browsers now have decent debugging tools at least some people now turn prompt off so that they don’t see it if people accidentally leave it in their live script.

You should use an HTML form to collect the data and an event listener to check for when the data has been entered.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.