Calculator Tools Calculator Tools
Create

Superhero Quiz

Dec 24, 2023

About this app

Test your knowledge about superheroes with this fun quiz app.

Superhero Quiz Superhero Quiz

Related apps

Put this on your site

Source

                    <html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">

<title>Superhero Quiz</title>
<meta name="description" content="Test your knowledge about superheroes with this fun quiz app.">
<meta name="keywords" content="superhero, quiz, knowledge, fun">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">

<script type="text/javascript">
try {
// App Javascript Goes Here. Place your entire script content inside the try block for error handling.

// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
// Your code here
});

} catch (error) {
// This will throw the error to the parent window.
throw error;
}
</script>

<style>
/* App CSS Goes Here */
body {
font-family: 'Roboto', sans-serif;
background-color: #f2f2f2;
color: #333;
}

.container {
max-width: 500px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

h1 {
text-align: center;
margin-bottom: 20px;
}

#question {
font-size: 20px;
margin-bottom: 20px;
text-align: center;
}

.options {
display: flex;
flex-direction: column;
gap: 10px;
}

.option {
padding: 10px;
border-radius: 5px;
background-color: #eee;
cursor: pointer;
}

.option:hover {
background-color: #ddd;
}

.option.selected {
background-color: #007bff;
color: #fff;
}

.result {
font-size: 18px;
text-align: center;
margin-top: 20px;
display: none;
}

.result.correct {
color: green;
}

.result.incorrect {
color: red;
}
</style>
</head>
<body>
<div id="main-container" class="container">
<h1>Superhero Quiz</h1>
<div id="question"></div>
<div class="options"></div>
<div class="result"></div>
</div>

<script type="text/javascript">
// App Javascript Goes Here. Place your entire script content here.

const questions = [
{
question: "What is the real name of Batman?",
options: ["Bruce Wayne", "Clark Kent", "Peter Parker", "Tony Stark"],
answer: "Bruce Wayne"
},
{
question: "Which superhero has a shield that is indestructible?",
options: ["Superman", "Captain America", "Spider-Man", "Wolverine"],
answer: "Captain America"
},
{
question: "Who is the alter ego of the superhero Iron Man?",
options: ["Bruce Banner", "Steve Rogers", "Tony Stark", "Peter Parker"],
answer: "Tony Stark"
},
{
question: "What is the real name of Spider-Man?",
options: ["Peter Parker", "Bruce Wayne", "Clark Kent", "Tony Stark"],
answer: "Peter Parker"
},
{
question: "Which superhero is known as the 'Man of Steel'?",
options: ["Superman", "Captain America", "Spider-Man", "Wolverine"],
answer: "Superman"
}
];

let currentQuestion = 0;
let score = 0;
let selectedOption = null;

function loadQuestion() {
const questionContainer = document.getElementById("question");
const optionsContainer = document.querySelector(".options");
const resultContainer = document.querySelector(".result");

questionContainer.innerText = questions[currentQuestion].question;
optionsContainer.innerHTML = "";

questions[currentQuestion].options.forEach((option) => {
const optionElement = document.createElement("div");
optionElement.classList.add("option");
optionElement.innerText = option;
optionElement.addEventListener("click", selectOption);
optionsContainer.appendChild(optionElement);
});

resultContainer.style.display = "none";
}

function selectOption() {
const options = document.querySelectorAll(".options .option");

options.forEach((option) => {
option.classList.remove("selected");
});

this.classList.add("selected");
selectedOption = this.innerText;
}

function checkAnswer() {
const resultContainer = document.querySelector(".result");

if (selectedOption === questions[currentQuestion].answer) {
resultContainer.innerText = "Correct!";
resultContainer.classList.add("correct");
score++;
} else {
resultContainer.innerText = "Incorrect!";
resultContainer.classList.add("incorrect");
}

resultContainer.style.display = "block";
}

function nextQuestion() {
if (currentQuestion < questions.length - 1) {
currentQuestion++;
loadQuestion();
} else {
showResult();
}
}

function showResult() {
const questionContainer = document.getElementById("question");
const optionsContainer = document.querySelector(".options");
const resultContainer = document.querySelector(".result");

questionContainer.innerText = `You scored ${score} out of ${questions.length}!`;
optionsContainer.innerHTML = "";
resultContainer.style.display = "none";
}

// This will run when the DOM is ready.
document.addEventListener("DOMContentLoaded", function() {
loadQuestion();
});
</script>
</body>
</html>