Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
<h2>Welcome to Javascript Quiz</h2>

<div id="container">
  <div id="questions">
  <p></p>
  </div>

  <p id="options">


  </p>
</div>
              
            
!

CSS

              
                
              
            
!

JS

              
                var quiz
let thequestion

function Quiz(questions) {
  this.questions = questions
  this.score = 0
  this.questionIndex = 0
}

Quiz.prototype.getQuestionIndex = function() {
  return this.questions[this.questionIndex]
}

Quiz.prototype.guess = function(answer) {
  if (this.getQuestionIndex().isCorrectAnswer(answer)) {
    this.score++
  }
  this.questionIndex++
}

Quiz.prototype.isEnded = function() {
  return this.questionIndex === this.questions.length
}

function Question(theQuestion, theChoices, theCorrectAnswer) {
  this.theQuestion = theQuestion
  this.theChoices = theChoices
  this.theCorrectAnswer = theCorrectAnswer
}

Question.prototype.isCorrectAnswer = function(choice) {
  return this.theCorrectAnswer === choice
}

function populate() {
  if (quiz.isEnded()) {
    console.log("finished")
    showScores()
  } else {
    var questionTextField = document.getElementById("questions")
    questionTextField.innerHTML = quiz.getQuestionIndex().theQuestion

    var choicesField = document.getElementById("options")
    var choices = quiz.getQuestionIndex().theChoices

    var ul = document.createElement("UL")
    choicesField.appendChild(ul)

    for (var i = 0; i < choices.length; i++) {
      var li = document.createElement("LI")
      li.setAttribute("data", choices[i])

      ul.appendChild(li)
      guess(choices[i])
      li.innerHTML = choices[i]
    }

    ul.addEventListener("click", function() {
      choicesField.removeChild(ul)
    })
    console.log("Current score" + " " + quiz.score)
  }
}

function guess(guess) {
  var optionsToClick = document.getElementById("options")
  optionsToClick.onclick = function(e) {
    quiz.guess(e.target.getAttribute("data"))
    populate()
  }
}

function showScores() {
  var gameOverHTML = "<h1>Result</h1>"
  gameOverHTML += "<h2 id='score'> Your scores: " + quiz.score + "</h2>"
  var element = document.getElementById("container")
  element.innerHTML = gameOverHTML
}

axios.get('/api/ninjas')
    .then(function (questions) {
      var data = questions.data;
  //console.log(questions);

  $.each(data, function(index, value) {
  
    thequestion = [
      new Question(value.the_question, value.the_choices, value.the_answer)

    ]

    quiz = new Quiz(thequestion)
    populate()
  })
}), 2000});

//Old data for quiz, when I wasn't using MongoDB. Worked fine.

/*let thequestion = [
  new Question("Which one of the three is a programming language?", ["Javascript", "HTML", "CSS"], "Javascript"),
  new Question("Is NodeJS is a front end or back end framework?", ["Back end", "Front End"], "Back end"),
  new Question("Is JAVA object oriented language?", ["Yes", "No"], "Yes")
]*/

//var quiz = new Quiz(thequestion);

              
            
!
999px

Console