Quiz
JavaScript Quiz. Question with 3 answers and total score.
HTML
<!-- in head -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap-reboot.min.css">
<!-- in body -->
<div class="view">
<div class="quiz">
<div class="title">
Test Personality
</div>
<hr>
<div class="question-box">
<div class="question-txt">
</div>
<label class="option">
<input type="checkbox" name="option" value="1" onclick="selectOnlyThis(this)"/>
<span class="option1"></span>
</label>
<label class="option">
<input type="checkbox" name="option" value="2" onclick="selectOnlyThis(this)"/>
<span class="option2"></span>
</label>
<label class="option">
<input type="checkbox" name="option" value="3" onclick="selectOnlyThis(this)"/>
<span class="option3"></span>
</label>
<div class="controls nav-box">
<button type="button" class="previous">Vorige</button>
<button type="button" class="next">Volgende</button>
</div>
</div>
<div class="result-box">
<div class="result-txt">
</div>
<div class="controls reset-box">
<button type="button" class="restart">Opnieuw</button>
</div>
</div>
</div><!--end quiz-->
</div><!--end view-->
CSS
html, body {
height: 100%;
}
body {
font-family: "Roboto", sans-serif;
font-weight: 400;
}
.view {
min-height: 100%;
padding-top: 2rem;
background: no-repeat linear-gradient(250deg, #ff934f, #db222a);
}
.quiz {
width: 40%;
margin: 0 auto;
padding: 25px 40px 40px 40px;
box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.2);
text-shadow: 1px 1px white;
text-shadow: 0 1px black;
background: linear-gradient(360deg, gray, white);
border-radius: 5px;
}
.quiz .title {
margin-bottom: 20px;
padding: 1rem;
color: black;
font-size: 2.5rem;
text-align: left;
background-color: gray;
border-radius: 5px 5px 0 0;
}
/* question */
.quiz .question {
margin: 10px 0;
font-size: 1.5rem;
}
/* option */
.quiz .option {
width: 100%;
padding: 1rem;
color: black;
background: rgba(255, 255, 255, 0.1);
border-radius: 5px;
transition: all 0.3s;
font-size: 20px;
cursor: pointer;
}
.quiz .option:hover {
background: rgba(255, 255, 255, 0.4);
}
.quiz .option input {
width: 20px;
height: 20px;
margin: -1px 4px 0 0;
vertical-align: middle;
background: url(check_radio_sheet.png) -38px top no-repeat;
cursor: pointer;
}
.quiz .option input:checked + span {
color: #c00;
}
/* buttons */
.quiz button {
margin-right: 10px;
padding: 5px 12px;
border: 2px white solid;
border-radius: 5px;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
font-weight: 400;
background: #e14;
color: white;
outline: none;
transform: scale(0.98);
transition: all 0.2s;
}
.quiz .controls {
margin: 1rem 0;
}
/* result */
.quiz .result {
padding: 1rem 0 0.5rem 0;
text-align: center;
font-size: 1.5rem;
}
.quiz .result h1 {
font-size: 2rem;
color: 222;
}
/* result summary */
.quiz .summary {
padding: 1rem;
font-size: 1rem;
text-shadow: 1px 1px #ffffff50;
color: #222;
background: rgba(255, 255, 255, 0.4);
border-radius: 5px;
}
JavaScript
const questions = [
{
"question" : "Wat is jouw IQ?",
"answer1" : "under 18",
"answer2" : "18 - 30",
"answer3" : "over 30",
"total1" : 8,
"total2" : 10,
"total3" : 12,
},
{
"question" :
"You make plans with a friend and they cancel on you, what do you do?",
"answer1" :
"Say \"whatever\" and plan a night that'll be GREAT so they don't cancel again.",
"answer2" : "Feel hurt because you were looking forward to tonight.",
"answer3" : "No problem, you kinda wanted to stay home anyway.",
"total1" : 5,
"total2" : 7,
"total3" : 9,
},
{
"question" : "Which of the following colours do you like most?",
"answer1" : "Black",
"answer2" : "Yellow or light blue",
"answer3" : "Red or orange",
"total1" : 4,
"total2" : 6,
"total3" : 8,
},
];
const questionBox = document.querySelector('.question-box');
const resultBox = document.querySelector('.result-box');
const resultTxt = document.querySelector('.result-txt');
const questionTxt = document.querySelector('.question-txt');
const option1 = document.querySelector('.option1');
const option2 = document.querySelector('.option2');
const option3 = document.querySelector('.option3');
const navBox = document.querySelector('.nav-box');
const resetBox = document.querySelector('.reset-box');
const nextButton = document.querySelector('.next');
const prevButton = document.querySelector('.previous');
const resetButton = document.querySelector('.restart');
const totalQuestions = questions.length;
let currentQuestion = 0;
// array to remember 'input value' of each question
let arrAnswer = [];
// reset all to default
function resetAll () {
questionBox.style.display = "block";
resultBox.style.display = "none";
nextButton.textContent = 'Volgende';
for (let i=0; i<totalQuestions; i++) {
arrAnswer[i] = 0;
}
}
// show question with 3 answers
function generateQuestions (index) {
const qItem = questions[index];
questionTxt.innerHTML = (index + 1)...