Skip to content

50Projects-HTML-CSS-JavaScript : Quiz app #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,17 @@ In order to run this project you need:
</details>
</li>

<li>
<details>
<summary>Quiz App</summary>
<p>A simple quiz app built using HTML, CSS, and JavaScript. The app presents multiple-choice questions, and the user can select answers to test their knowledge. At the end of the quiz, the user's score is displayed along with an option to restart the quiz.</p>
<ul>
<li><a href="https://tajulafreen.github.io/50Projects-HTML-CSS-JavaScript/Source-Code/QuizApp/">Live Demo</a></li>
<li><a href="https://github.com/tajulafreen/50Projects-HTML-CSS-JavaScript/tree/main/Source-Code/Quizapp">Source</a></li>
</ul>
</details>
</li>

</ol>

<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand Down
35 changes: 35 additions & 0 deletions Source-Code/QuizApp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!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="style.css">
</head>
<body>
<div class="quiz-container">
<h1>Quiz App</h1>
<div id="quiz-box">
<div id="question-container">
<p id="question">Loading question...</p>
</div>
<div id="answer-container">
<button class="answer-btn">Answer 1</button>
<button class="answer-btn">Answer 2</button>
<button class="answer-btn">Answer 3</button>
<button class="answer-btn">Answer 4</button>
</div>
<div id="next-button-container">
<button id="next-btn" disabled>Next</button>
</div>
</div>
<div id="result-container" class="hidden">
<h2>Quiz Completed!</h2>
<p id="score"></p>
<button id="restart-btn">Restart</button>
</div>
</div>

<script src="script.js"></script>
</body>
</html>
107 changes: 107 additions & 0 deletions Source-Code/QuizApp/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const questions = [
{
question: 'What is the capital of France?',
answers: ['Berlin', 'Madrid', 'Paris', 'Rome'],
correct: 2,
},
{
question: 'Which planet is known as the Red Planet?',
answers: ['Earth', 'Mars', 'Jupiter', 'Saturn'],
correct: 1,
},
{
question: 'What is the largest ocean on Earth?',
answers: ['Atlantic', 'Indian', 'Arctic', 'Pacific'],
correct: 3,
},
{
question: "Who wrote 'Romeo and Juliet'?",
answers: ['Shakespeare', 'Dickens', 'Hemingway', 'Austen'],
correct: 0,
},
];

let currentQuestionIndex = 0;
let score = 0;

const questionElement = document.getElementById('question');
const answerButtons = document.querySelectorAll('.answer-btn');
const nextButton = document.getElementById('next-btn');
const resultContainer = document.getElementById('result-container');
const restartButton = document.getElementById('restart-btn');
const scoreElement = document.getElementById('score');

// Load question and answers
const loadQuestion = () => {
const currentQuestion = questions[currentQuestionIndex];
questionElement.textContent = currentQuestion.question;

answerButtons.forEach((button, index) => {
button.textContent = currentQuestion.answers[index];
button.disabled = false;
button.style.backgroundColor = '#4CAF50'; // Reset button color
});

nextButton.disabled = true; // Disable next button until an answer is selected
};

// Handle answer selection
const handleAnswerClick = (index) => {
const currentQuestion = questions[currentQuestionIndex];

if (index === currentQuestion.correct) {
score += 1;
}

// Disable all buttons after an answer is selected
answerButtons.forEach((button, i) => {
button.disabled = true;
if (i === currentQuestion.correct) {
button.style.backgroundColor = '#4CAF50'; // Correct answer
} else {
button.style.backgroundColor = '#f44336'; // Incorrect answers
}
});

nextButton.disabled = false; // Enable next button
};

// Show quiz result
const showResult = () => {
document.getElementById('quiz-box').classList.add('hidden');
resultContainer.classList.remove('hidden');
scoreElement.textContent = `You scored ${score} out of ${questions.length}`;
};

// Move to next question
const nextQuestion = () => {
currentQuestionIndex += 1;
if (currentQuestionIndex < questions.length) {
loadQuestion();
} else {
showResult();
}
};

// Restart the quiz
const restartQuiz = () => {
currentQuestionIndex = 0;
score = 0;
resultContainer.classList.add('hidden');
document.getElementById('quiz-box').classList.remove('hidden');
loadQuestion();
};

// Add event listeners to answer buttons
answerButtons.forEach((button, index) => {
button.addEventListener('click', () => handleAnswerClick(index));
});

// Add event listener to next button
nextButton.addEventListener('click', nextQuestion);

// Add event listener to restart button
restartButton.addEventListener('click', restartQuiz);

// Start the quiz
loadQuestion();
84 changes: 84 additions & 0 deletions Source-Code/QuizApp/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* styles.css */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}

.quiz-container {
background: #fff;
border-radius: 8px;
padding: 20px;
width: 300px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

h1 {
text-align: center;
color: #333;
}

#question-container {
margin-bottom: 20px;
}

#answer-container {
margin-bottom: 20px;
}

.answer-btn {
background-color: #4caf50;
color: white;
padding: 10px;
width: 100%;
margin: 5px 0;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}

.answer-btn:hover {
background-color: #45a049;
}

#next-btn {
background-color: #008cba;
color: white;
padding: 10px;
width: 100%;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}

#next-btn:disabled {
background-color: #ccc;
}

.hidden {
display: none;
}

#result-container {
text-align: center;
}

#restart-btn {
background-color: #f44336;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
width: 100%;
}

#restart-btn:hover {
background-color: #d32f2f;
}