JSFiddle - React, Tailwind, and code Playground
HTML
<div id="prize"></div>
JavaScript
var correctAnswers = 0,
questionsAsked = 0,
question = [
"Question 1: Who wrote the classic early nineties DC Comics graphic novel 'Kingdom Come'?",
"Question 2: In what state is Superman's fictional hometown of Smallville located?",
"Question 3: Frank Einstein is the alter ego of what snappy Image Comics superhero created by Mike Allred?",
"Question 4: Who took over the mantle of The Flash after Barry Allen's demise in the Crisis on Infinite Earths storyline?",
"Question 5: Which actor portrayed Superman in the 2013 motion picture 'Man of Steel'?"
],
answer = [
"MARK WAID",
"KANSAS",
"MADMAN",
"WALLY WEST",
"HENRY CAVILL"
];
function askQuestion(){
var i, userAnswer;
//LOOP THROUGH QUESTIONS
for(i = 0; i < question.length; i++){
userAnswer = prompt(question[i]);
//IF USER CANCELS OR QUITS
if(userAnswer === null || userAnswer.toUpperCase() === "QUIT"){
//STOP LOOP
break;
}//IF USER ANSWER IS CORRECT
else if(userAnswer.toUpperCase() === answer[i]){
//INCREASE CORRECT ANSWERS COUNT
correctAnswers += 1;
//INCREASE QUESTIONS ASKED COUNT
questionsAsked += 1;
//ALERT ANSWER WAS CORRECT
alertMessage(i, 0, correctAnswers);
} else {
//INCREASE QUESTIONS ASKED COUNT
questionsAsked += 1;
//ALERT ANSWER WAS INCORRECT
alertMessage(i, 1, correctAnswers);
}
}
if (questionsAsked === 5){
givePrize(correctAnswers);
}
}
function alertMessage(question, answer, correctAnswers){
var message = [
[
"You got it right! You now have a score of " + correctAnswers + "! Here comes question #2",
"Sorry, dude, you got it wrong. Let's try another one."
],
[
"Nailed it! You now have a...