Online Quiz

HTML (index.html)

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;Online Quiz App&lt;/title&gt;
&lt;link rel="stylesheet" href="style.css"&gt;
</head> <body>
&lt;div class="container"&gt;
    &lt;h1&gt;Quiz App&lt;/h1&gt;
    &lt;div id="quiz"&gt;&lt;/div&gt;
    &lt;button id="submit" class="btn"&gt;Submit Quiz&lt;/button&gt;
    &lt;div id="result"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;script src="script.js"&gt;&lt;/script&gt;
</body> </html>

2. CSS (style.css)

cssCopy codebody {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
} .container {
max-width: 600px;
margin: auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
} h1 {
text-align: center;
} .question {
margin: 20px 0;
} .btn {
display: block;
width: 100%;
padding: 10px;
background: #28a745;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
} .btn:hover {
background: #218838;
}

3. JavaScript (script.js)

javascriptCopy codeconst quizData = [
{
    question: "What is the capital of France?",
    a: "Berlin",
    b: "Madrid",
    c: "Paris",
    d: "Lisbon",
    correct: "c"
},
{
    question: "Who wrote 'To Kill a Mockingbird'?",
    a: "Harper Lee",
    b: "Mark Twain",
    c: "F. Scott Fitzgerald",
    d: "Ernest Hemingway",
    correct: "a"
},
{
    question: "What is the largest planet in our solar system?",
    a: "Earth",
    b: "Mars",
    c: "Jupiter",
    d: "Saturn",
    correct: "c"
},
{
    question: "What is the smallest prime number?",
    a: "0",
    b: "1",
    c: "2",
    d: "3",
    correct: "c"
}
]; const quizContainer = document.getElementById('quiz'); const submitButton = document.getElementById('submit'); const resultContainer = document.getElementById('result'); let currentQuestionIndex = 0; let score = 0; function loadQuiz() {
const currentQuizData = quizData&#91;currentQuestionIndex];
quizContainer.innerHTML = `
    &lt;div class="question"&gt;${currentQuizData.question}&lt;/div&gt;
    &lt;label&gt;&lt;input type="radio" name="answer" value="a"&gt; ${currentQuizData.a}&lt;/label&gt;&lt;br&gt;
    &lt;label&gt;&lt;input type="radio" name="answer" value="b"&gt; ${currentQuizData.b}&lt;/label&gt;&lt;br&gt;
    &lt;label&gt;&lt;input type="radio" name="answer" value="c"&gt; ${currentQuizData.c}&lt;/label&gt;&lt;br&gt;
    &lt;label&gt;&lt;input type="radio" name="answer" value="d"&gt; ${currentQuizData.d}&lt;/label&gt;&lt;br&gt;
`;
} function getSelected() {
const answers = document.querySelectorAll('input&#91;name="answer"]');
let answer;
answers.forEach((ans) =&gt; {
    if (ans.checked) {
        answer = ans.value;
    }
});
return answer;
} submitButton.addEventListener('click', () => {
const answer = getSelected();
if (answer) {
    if (answer === quizData&#91;currentQuestionIndex].correct) {
        score++;
    }
    currentQuestionIndex++;
    if (currentQuestionIndex &lt; quizData.length) {
        loadQuiz();
    } else {
        resultContainer.innerHTML = `
            &lt;h2&gt;You scored ${score} out of ${quizData.length}&lt;/h2&gt;
        `;
        quizContainer.style.display = 'none';
        submitButton.style.display = 'none';
    }
} else {
    alert('Please select an answer!');
}
}); loadQuiz();

How to Run the Quiz App

  1. Create the files: Create three files named index.html, style.css, and script.js.
  2. Copy the code: Copy the respective code snippets into the appropriate files.
  3. Open the HTML file: Open index.html in your web browser to see the quiz app in action.

Features You Can Add

  • Multiple choice with more than one correct answer.
  • Timer for each question.
  • Different categories of questions.
  • User authentication to save scores.
  • Database integration for a larger question pool.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *