2022-02-06 21:30:08 -05:00
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html lang="en">
|
2025-07-15 17:41:52 +01:00
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
|
<link rel="stylesheet" href="./style.css" />
|
2022-02-06 21:30:08 -05:00
|
|
|
<title>Quiz</title>
|
|
|
|
|
<style>
|
2025-07-15 17:41:52 +01:00
|
|
|
@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap");
|
|
|
|
|
|
|
|
|
|
* {
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
margin: 0;
|
|
|
|
|
padding: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html,
|
|
|
|
|
body {
|
|
|
|
|
font-family: "Open Sans", sans-serif;
|
|
|
|
|
line-height: 1.4;
|
|
|
|
|
background-color: #333;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.main {
|
|
|
|
|
margin: 0 auto;
|
|
|
|
|
width: 600px;
|
|
|
|
|
margin-top: 200px;
|
|
|
|
|
|
|
|
|
|
background-color: white;
|
|
|
|
|
padding: 20px;
|
|
|
|
|
border-radius: 10px;
|
|
|
|
|
/* box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px; */
|
|
|
|
|
box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px,
|
|
|
|
|
rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.hidden {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
button {
|
|
|
|
|
padding: 10px;
|
|
|
|
|
border: 10px;
|
|
|
|
|
background-color: teal;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
color: white;
|
|
|
|
|
border-radius: 2px;
|
|
|
|
|
}
|
|
|
|
|
button:hover {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
input {
|
|
|
|
|
margin: 10px;
|
|
|
|
|
border: none;
|
|
|
|
|
font-size: large;
|
|
|
|
|
color: rgba(0, 128, 128, 0.616);
|
|
|
|
|
}
|
|
|
|
|
textarea {
|
|
|
|
|
border: none;
|
|
|
|
|
font-size: large;
|
|
|
|
|
color: rgba(0, 128, 128, 0.616);
|
|
|
|
|
}
|
|
|
|
|
ul {
|
|
|
|
|
list-style: none;
|
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
}
|
|
|
|
|
#bool {
|
|
|
|
|
list-style: none;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
margin: 10px;
|
|
|
|
|
}
|
|
|
|
|
textarea:focus,
|
|
|
|
|
input:focus {
|
|
|
|
|
outline: none;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#yourAnswers p {
|
|
|
|
|
margin-top: 30px;
|
|
|
|
|
color: teal;
|
|
|
|
|
}
|
2022-02-06 21:30:08 -05:00
|
|
|
</style>
|
2025-07-15 17:41:52 +01:00
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="main">
|
|
|
|
|
<h1>Quiz</h1>
|
|
|
|
|
<div id="yourAnswers" class="hidden">
|
|
|
|
|
<h1>Your answers</h1>
|
|
|
|
|
<pre id="answerObject"></pre>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="question" id="questionsDiv">
|
|
|
|
|
<h2 id="title"></h2>
|
|
|
|
|
<ul id="selectionList"></ul>
|
|
|
|
|
<input id="short" type="text" class="hidden" />
|
|
|
|
|
<textarea
|
|
|
|
|
name=""
|
|
|
|
|
id="long"
|
|
|
|
|
class="hidden"
|
|
|
|
|
cols="30"
|
|
|
|
|
rows="10"
|
|
|
|
|
></textarea>
|
|
|
|
|
<div id="bool"></div>
|
|
|
|
|
<button id="next">Next</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2022-02-06 21:30:08 -05:00
|
|
|
|
|
|
|
|
<script>
|
2025-07-15 17:41:52 +01:00
|
|
|
// Fetch quiz data from API
|
|
|
|
|
fetch("/api/quiz")
|
|
|
|
|
.then((res) => res.json())
|
|
|
|
|
.then((result) => {
|
|
|
|
|
if (result.success) {
|
|
|
|
|
// Transform API data to match expected quiz array structure
|
|
|
|
|
const apiQuiz = result.data;
|
|
|
|
|
window.quiz = apiQuiz.questions.map((q, idx) => ({
|
|
|
|
|
id: q.id,
|
|
|
|
|
type: "multiple_choice", // You can enhance this to support more types if needed
|
|
|
|
|
question: q.questionText,
|
|
|
|
|
answers: q.options.map((opt, i) => ({ id: i + 1, answer: opt })),
|
|
|
|
|
correct_answer: q.answer,
|
|
|
|
|
}));
|
|
|
|
|
// Start quiz
|
|
|
|
|
window.i = 0;
|
|
|
|
|
window.answers = {};
|
|
|
|
|
populateQuestion(window.i);
|
|
|
|
|
} else {
|
|
|
|
|
alert("Failed to load quiz: " + result.error);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
alert("Error fetching quiz: " + err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let nextButton = document.getElementById("next");
|
|
|
|
|
let title = document.getElementById("title");
|
|
|
|
|
let selectionList = document.getElementById("selectionList");
|
|
|
|
|
let bool = document.getElementById("bool");
|
|
|
|
|
let inp = document.getElementById("short");
|
|
|
|
|
let text = document.getElementById("long");
|
|
|
|
|
let yourAnswers = document.getElementById("yourAnswers");
|
|
|
|
|
let questionsDiv = document.getElementById("questionsDiv");
|
|
|
|
|
|
|
|
|
|
//step
|
|
|
|
|
nextButton.onclick = function () {
|
|
|
|
|
//get the answers
|
|
|
|
|
if (quiz[i].type == "short_answer") {
|
|
|
|
|
let x = document.getElementsByClassName("current")[0];
|
|
|
|
|
let currentAnswer = x.value;
|
|
|
|
|
let question = quiz[i].question;
|
|
|
|
|
let answer = { [question]: currentAnswer };
|
|
|
|
|
answers = { ...answers, [quiz[i].id]: { ...answer } };
|
|
|
|
|
x.classList.toggle("hidden");
|
|
|
|
|
x.classList.remove("current");
|
|
|
|
|
} else if (
|
|
|
|
|
quiz[i].type == "multiple_choice" ||
|
|
|
|
|
quiz[i].type == "multiple_selection_choice"
|
|
|
|
|
) {
|
|
|
|
|
let x = document.querySelector(`input[name="question${i}_choice"]`);
|
|
|
|
|
let currentAnswer = x.value;
|
|
|
|
|
let question = quiz[i].question;
|
|
|
|
|
let answer = { [question]: currentAnswer };
|
|
|
|
|
answers = { ...answers, [quiz[i].id]: { ...answer } };
|
|
|
|
|
x.classList.toggle("hidden");
|
|
|
|
|
} else if (
|
|
|
|
|
quiz[i].type == "long_text" ||
|
|
|
|
|
quiz[i].type == "description"
|
|
|
|
|
) {
|
|
|
|
|
let x = document.getElementsByClassName("current")[0];
|
|
|
|
|
let currentAnswer = x.value;
|
|
|
|
|
let question = quiz[i].question;
|
|
|
|
|
let answer = { [question]: currentAnswer };
|
|
|
|
|
answers = { ...answers, [quiz[i].id]: { ...answer } };
|
|
|
|
|
x.classList.toggle("hidden");
|
|
|
|
|
x.classList.remove("current");
|
|
|
|
|
} else if (quiz[i].type == "true_false") {
|
|
|
|
|
let x = document.querySelector("input[name=bool]");
|
|
|
|
|
let currentAnswer = x.value;
|
|
|
|
|
let question = quiz[i].question;
|
|
|
|
|
let answer = { [question]: currentAnswer };
|
|
|
|
|
answers = { ...answers, [quiz[i].id]: { ...answer } };
|
|
|
|
|
x.classList.toggle("hidden");
|
|
|
|
|
}
|
|
|
|
|
console.log(i);
|
|
|
|
|
++i;
|
|
|
|
|
if (i > quiz.length - 1) {
|
|
|
|
|
//display answer
|
|
|
|
|
questionsDiv.classList.toggle("hidden");
|
|
|
|
|
yourAnswers.classList.toggle("hidden");
|
|
|
|
|
const myJSON = JSON.stringify(answers, null, "\t");
|
|
|
|
|
document.getElementById("answerObject").innerHTML = myJSON;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
populateQuestion(i);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//generate question
|
|
|
|
|
function populateQuestion(qNum) {
|
|
|
|
|
let individualQuestion = quiz[qNum];
|
|
|
|
|
title.innerText = individualQuestion.question;
|
|
|
|
|
|
|
|
|
|
//condition to check type of question
|
|
|
|
|
|
|
|
|
|
if (individualQuestion.type == "short_answer") {
|
|
|
|
|
selectionList.innerHTML = "";
|
|
|
|
|
short.classList.toggle("hidden");
|
|
|
|
|
short.classList.add("current");
|
|
|
|
|
short.placeholder = "your reason here";
|
|
|
|
|
} else if (
|
|
|
|
|
individualQuestion.type == "multiple_choice" ||
|
|
|
|
|
individualQuestion.type == "multiple_selection_choice"
|
|
|
|
|
) {
|
|
|
|
|
selectionList.innerHTML = ""; //reset choices list
|
|
|
|
|
for (j in individualQuestion.answers) {
|
|
|
|
|
let radioBtnName = "question" + qNum + "_choice";
|
|
|
|
|
let choiceText = individualQuestion.answers[j].answer;
|
|
|
|
|
selectionList.appendChild(createLi(radioBtnName, choiceText));
|
|
|
|
|
}
|
|
|
|
|
} else if (
|
|
|
|
|
individualQuestion.type == "long_text" ||
|
|
|
|
|
individualQuestion.type == "description"
|
|
|
|
|
) {
|
|
|
|
|
selectionList.innerHTML = "";
|
|
|
|
|
long.classList.toggle("hidden");
|
|
|
|
|
long.classList.add("current");
|
|
|
|
|
long.placeholder = "your reason here";
|
|
|
|
|
} else if (individualQuestion.type == "true_false") {
|
|
|
|
|
selectionList.innerHTML = "";
|
|
|
|
|
bool.appendChild(createLi("bool", "True"));
|
|
|
|
|
bool.appendChild(createLi("bool", "True"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//multiple choice
|
|
|
|
|
function createLi(name, choiceText) {
|
|
|
|
|
let e = document.createElement("li");
|
|
|
|
|
let radioHtml =
|
|
|
|
|
'<input value="' + choiceText + '" type="radio" name="' + name + '"';
|
|
|
|
|
radioHtml += "/>";
|
|
|
|
|
radioHtml += choiceText;
|
|
|
|
|
e.innerHTML = radioHtml;
|
|
|
|
|
return e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//call function at the beginning
|
|
|
|
|
populateQuestion(i);
|
2022-02-06 21:30:08 -05:00
|
|
|
</script>
|
2025-07-15 17:41:52 +01:00
|
|
|
</body>
|
|
|
|
|
</html>
|