Professional Documents
Culture Documents
Codeday Workshop
Codeday Workshop
Workshop
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Interactive Quiz</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="quiz-container">
<img src="/assets/codeday-notext-color.png" alt="CodeDay logo" width="100px">
<h3>CodeDay Interactive Quiz</h3>
<div class="question-container">
<h1 id="question">Question goes here</h1>
<div class="options-container">
<button class="option">Option 1</button>
<button class="option">Option 2</button>
<button class="option">Option 3</button>
<button class="option">Option 4</button>
</div>
<button class="btn" id="submit-btn">Submit</button>
</div>
<script src="script.js"></script>
</body>
</html>
The HTML structure starts with the <!DOCTYPE html> declaration, indicating an
HTML5 document.
Inside the body, there is a main container div with the class quiz-container .
Within the quiz-container , there's an image element for the CodeDay logo and an
<h3> heading for the quiz title.
The quiz questions and options are contained within the question-container div.
The options are displayed inside a div with the class options-container , containing
four <button> elements with the class "option".
At the bottom of the question container, there's a submit button with the id "submit-
btn".
The result container is initially hidden and contains the score and a restart button.
The result container has the class "result-container" and contains an <h2> element
to display the score, as well as a restart button with the id "restart-btn".
Finally, the <script> tag at the end of the body links the JavaScript file, "script.js", to
the HTML.
CSS:
body {
display: grid;
place-items: center;
height: 100vh;
margin: 0;
}
.quiz-container {
max-width: 500px;
text-align: center;
padding: 20px;
}
h1 {
color: #333;
}
.question-container {
.options-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
margin-bottom: 20px;
}
.option {
padding: 10px;
background-color: #eee;
border: none;
cursor: pointer;
}
.option:hover {
background-color: #ccc;
}
.checked {
background-color: #bada55;
}
.checked:hover {
background-color: #bada55;
}
.btn {
padding: 10px 20px;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
transition: background-color 0.3s ease;
}
.btn:hover {
background-color: #555;
}
.result-container {
font-weight: bold;
margin-top: 20px;
}
.hidden {
display: none;
}
The CSS code provides the styling for various elements in the HTML.
The .quiz-container class sets the maximum width of the quiz container to 500
pixels and centers the text within it.
The .question-container class adds a margin at the bottom of the question container.
The .options-container class utilizes grid layout to display the answer options in a
grid with two columns. The gap property adds a 10-pixel gap between the options.
The .option class defines the styling for each option button, including padding,
background color, and cursor behavior.
The .option:hover selector changes the background color of an option button when
hovering over it.
The .checked and .checked:hover classes define the styles for the selected option
button.
The .btn class sets the styles for the submit and restart buttons, including padding,
background color, and cursor behavior. The transition property adds a smooth
transition effect when hovering over the buttons.
The .btn:hover selector changes the background color of a button when hovering
over it.
The .result-container class sets the font weight to bold and adds a margin at the
top.
The .hidden class is used to hide elements by setting their display property to
"none".
JavaScript:
// Quiz data
const quizData = [
{
question: "What is the capital city of Ethiopia?",
options: ["Addis Ababa", "Nairobi", "Kampala", "Dar es Salaam"],
correctAnswer: 0,
},
let score = 0;
let currentQuestion = 0;
optionsElements.forEach((option) => {
option.addEventListener("click", (e) => {
optionsElements.forEach((option) => {
option.classList.remove("checked");
});
option.classList.add("checked");
});
});
selectedOption.classList.remove("checked");
// Show result
function showResult() {
questionContainer.classList.add("hidden");
resultContainer.classList.remove("hidden");
scoreElement.textContent = score;
totalElement.textContent = quizData.length;
restartButton.addEventListener("click", () => {
questionContainer.classList.remove("hidden");
resultContainer.classList.add("hidden");
score = 0;
currentQuestion = 0;
loadQuestion();
});
}
The JavaScript code begins by defining the quiz data as an array of objects. Each
object represents a question and its corresponding options and correct answer.
Inside loadQuestion , the current question is accessed from the quizData array using
the currentQuestion index.
Another forEach loop is used to attach click event listeners to each option button.
When an option is clicked, the checked class is removed from all options, and then
added only to the clicked option.
The checkAnswer function is called when the submit button is clicked. It first checks if
an option has been selected by querying for the button with the class option and
checked . If no option is selected, the function returns early.
The selected answer is obtained from the data-answer attribute of the selected
option button and converted to a number using Number() .
If the selected answer matches the correct answer for the current question, the
score variable is incremented.
The currentQuestion index is incremented, and the checked class is removed from
the selected option button.
If there are more questions remaining, loadQuestion is called to load the next
question. Otherwise, the showResult function is called to display the final score.
The showResult function hides the question container and shows the result
container. It sets the text content of the scoreElement to the current score and the
totalElement to the total number of questions.
An event listener is added to the restart button to reset the quiz when clicked. It
removes the hidden class from the question container and adds it to the result
container. Additionally, the score and currentQuestion variables are reset, and
loadQuestion is called to load the initial question.
Finally, an event listener is added to the submit button to call the checkAnswer
function when clicked, and the initial question is loaded using loadQuestion .