Below is a simple implementation of a quiz app using pure HTML, CSS, and vanilla JavaScript. The app includes 30 questions, displays the result with percentage, correct/incorrect answers, and prevents restarting the quiz for 2 hours after completion.
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="quiz-container" id="quizContainer">
<h1>Quiz App</h1>
<div id="questionContainer"></div>
<button id="submitBtn" onclick="submitQuiz()">Submit</button>
</div>
<div class="result-container" id="resultContainer" style="display: none;">
<h2>Quiz Result</h2>
<p id="score"></p>
<p id="percentage"></p>
<div id="correctAnswers"></div>
<div id="incorrectAnswers"></div>
<button id="restartBtn" onclick="restartQuiz()" disabled>Restart Quiz</button>
<p id="restartTimer"></p>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.quiz-container, .result-container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: 400px;
text-align: left;
}
button {
padding: 10px 20px;
background-color: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 20px;
}
button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.question {
margin-bottom: 20px;
}
JavaScript (script.js)
const questions = [
// Add 30 questions here in the following format:
{
question: "What does HTML stand for?",
options: ["Hyper Text Markup Language", "Hyperlinks and Text Markup Language", "Home Tool Markup Language"],
answer: "Hyper Text Markup Language"
},
// Add 29 more questions...
];
let quizContainer = document.getElementById("quizContainer");
let resultContainer = document.getElementById("resultContainer");
let questionContainer = document.getElementById("questionContainer");
let submitBtn = document.getElementById("submitBtn");
let restartBtn = document.getElementById("restartBtn");
let restartTimer = document.getElementById("restartTimer");
let userAnswers = [];
let timer;
function generateQuiz() {
questions.forEach((q, index) => {
let questionDiv = document.createElement("div");
questionDiv.className = "question";
questionDiv.innerHTML = `<p>${index + 1}. ${q.question}</p>`;
q.options.forEach((option, i) => {
let radioBtn = document.createElement("input");
radioBtn.type = "radio";
radioBtn.name = `question${index}`;
radioBtn.value = option;
radioBtn.id = `q${index}o${i}`;
let label = document.createElement("label");
label.htmlFor = `q${index}o${i}`;
label.textContent = option;
questionDiv.appendChild(radioBtn);
questionDiv.appendChild(label);
questionDiv.appendChild(document.createElement("br"));
});
questionContainer.appendChild(questionDiv);
});
}
function submitQuiz() {
userAnswers = [];
let allQuestions = document.querySelectorAll(".question");
let allAnswered = true;
allQuestions.forEach((q, index) => {
let selectedOption = q.querySelector(`input[name="question${index}"]:checked`);
if (!selectedOption) {
allAnswered = false;
} else {
userAnswers.push(selectedOption.value);
}
});
if (!allAnswered) {
alert("Please answer all questions!");
return;
}
let score = 0;
let correctAnswersHTML = "<h3>Correct Answers:</h3>";
let incorrectAnswersHTML = "<h3>Incorrect Answers:</h3>";
questions.forEach((q, index) => {
if (userAnswers[index] === q.answer) {
score++;
correctAnswersHTML += `<p>${index + 1}. ${q.question} - Your answer: ${userAnswers[index]}</p>`;
} else {
incorrectAnswersHTML += `<p>${index + 1}. ${q.question} - Your answer: ${userAnswers[index]} (Correct: ${q.answer})</p>`;
}
});
document.getElementById("score").textContent = `Score: ${score}/${questions.length}`;
document.getElementById("percentage").textContent = `Percentage: ${((score / questions.length) * 100).toFixed(2)}%`;
document.getElementById("correctAnswers").innerHTML = correctAnswersHTML;
document.getElementById("incorrectAnswers").innerHTML = incorrectAnswersHTML;
quizContainer.style.display = "none";
resultContainer.style.display = "block";
// Disable restart button for 2 hours
let now = new Date();
let restartTime = new Date(now.getTime() + 2 * 60 * 60 * 1000);
localStorage.setItem("restartTime", restartTime);
restartBtn.disabled = true;
timer = setInterval(updateTimer, 1000);
}
function updateTimer() {
let restartTime = new Date(localStorage.getItem("restartTime"));
let now = new Date();
let diff = restartTime - now;
if (diff <= 0) {
clearInterval(timer);
restartBtn.disabled = false;
restartTimer.textContent = "";
} else {
let hours = Math.floor(diff / (1000 * 60 * 60));
let minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((diff % (1000 * 60)) / 1000);
restartTimer.textContent = `You can restart the quiz in: ${hours}h ${minutes}m ${seconds}s`;
}
}
function restartQuiz() {
quizContainer.style.display = "block";
resultContainer.style.display = "none";
questionContainer.innerHTML = "";
generateQuiz();
localStorage.removeItem("restartTime");
clearInterval(timer);
}
// Check if quiz was completed recently
window.onload = function() {
let restartTime = localStorage.getItem("restartTime");
if (restartTime) {
restartBtn.disabled = true;
timer = setInterval(updateTimer, 1000);
} else {
generateQuiz();
}
};
Notes:
- Questions Array: Replace the placeholder question with 30 actual questions in the
questions array.
- Local Storage: The app uses
localStorage to track when the quiz was last completed to enforce the 2-hour wait.
- Timer: The timer updates every second to show the remaining time until the quiz can be restarted.
- Responsive Design: The CSS is basic but can be improved for better responsiveness.
This implementation ensures the quiz is functional, shows results, and enforces the 2-hour wait before restarting.