JSFiddle - React, Tailwind, and code Playground
JavaScript
(function() {
"use strict";
var score = 0;
var quiz = [
["What is 2 + 3?", 5],
["What is 3 - 1?", 2],
["What is 8 - 2?", 6]
]
var correct = [];
var wrong = [];
function print(message) {
document.write(message);
}
function toAList(list) {
var htmlChange = '<ol>';
for (var x = 0; x < list.length; x++) {
htmlChange += "<li>" + list[x][0] + "</li>";
}
htmlChange += '</ol>'
print(htmlChange);
}
for (var i = 0; i < quiz.length; i++) {
var userAnswer = parseInt(prompt(quiz[i][0]));
if (userAnswer === quiz[i][1]) {
correct.push(quiz[i]);
score += 1;
} else {
wrong.push(quiz[i]);
score -= 1;
}
}
console.log(score);
print("You got " + correct.length + " question(s) correctly!<br>");
print("Your final score is " + score + "<br>");
print("<b>These are the questions you got correct:</b>")
toAList(correct);
print("<b>These are the questions you got wrong:</b>");
toAList(wrong);
}());