JSFiddle - React, Tailwind, and code Playground
HTML
<div id="block"></div>
JavaScript
var question = [
['What is the capital of UK?', 'london'],
['What is the biggest state in the USA?', 'alaska'],
['How many states in USA?', '50'],
['What is the most popular car in Japan?', 'toyota'],
['What is the capital of France', 'paris']
],
correct = 0,
incorrect = 0,
questionsAsked = 0,
correctAnswers = [],
incorrectAnswers = [],
i;
for (i = 0 ; i < question.length ; i++) {
//GET QUESTION
var q = question[i][0];
//ASK THE QUESTION
var askQ = prompt(q);
//GET THE ANSWER
var a = question[i][1];
if(askQ === null || askQ.toLowerCase() === "quit") {
return false;
} //IF ANSWER CORRECT
else if(askQ.toLowerCase() === a){
//ADD TO CORRECT COUNT
correct += 1;
//ADD TO CORRECT ANSWERS ARRAY
correctAnswers.push(q);
//TOTAL QUESTIONS ASKED
questionsAsked += 1;
} else {
//ADD TO INCORRECT COUNT
incorrect += 1;
//ADD TO INCORRECT ANSWERS ARRAY
incorrectAnswers.push(q);
//TOTAL QUESTIONS ASKED
questionsAsked += 1;
}
}
if(questionsAsked === question.length){
renderHTML();
}
function renderHTML() {
var html = '',
block = document.getElementById("block");
block.innerHTML = '<p>You Got ' + correct + ' Questions Right of ' + question.length + '</p>';
block.innerHTML += '<h2>You Got This Questions Correct:</h2>';
for(i = 0; i < correctAnswers.length; i++){
block.innerHTML += '<p>' + correctAnswers[i] + '</p>';
}
block.innerHTML += '<h2>You Got These Questions Wrong:</h2>'
for(i = 0; i < incorrectAnswers.length; i++){
block.innerHTML += '<p>'+ incorrectAnswers[i] +'</p>';
}
};