-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscript.js
90 lines (79 loc) · 3.45 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const delayTime = 2000; // wachttijd voor de volgende vraag
const myQuestion = document.getElementById('myQuestion');
const myAnswer = document.getElementById('myAnswer');
const quizWrapper = document.getElementById('quizWrapper');
const questionBox = document.getElementById('questionBox');
const resultBox = document.getElementById('resultBox');
const quizTitle = document.getElementById('quizTitle');
let counter = 0; // aantal mutliple choice vragen
let quiz; // object met quiz vragen
let playerData = {}; // object, hierin worden de game gegevens opgeslagen
let quizNummer = 1; // voorbereiden automatisch 2e quiz startem
function init(){
quiz = quiz1; // kies de quiz
// quiz = quiz2; // kies de quiz
initQuiz(); // start de quiz
}
function initQuiz(){
questionBox.style.display = "block"; // reset alle player game variabelen
resultBox.style.display = "none"; // reset alle player game variabelen
counter = 0; // reset alle player game variabelen
playerData.goodAnswers = 0; // reset alle player game variabelen
playerData.wrongAnswers = 0; // reset alle player game variabelen
playerName = ""; // toekomstige uitbreiding naam speler opvragen
resultBox.style.display = "none"; // verberg de resultbox
quizTitle.innerHTML=quiz.quizMetaData.title; // laat titel van quiz zien
prepareQuestions(); // start de quiz
}
function prepareQuestions() {
questionBox.className = "questionBox-new"; // voorbereiden animatie
let quizImage = quiz.quizMetaData.imageURI; // image laden
quizWrapper.style.backgroundImage = "url("+ quizImage + ")"; // image laden
quizWrapper.style.backgroundRepeat = "no-repeat"; // image positioneren
quizWrapper.style.backgroundPosition = "right"; // image positioneren
quizWrapper.style.backgroundSize = "25%"; // image positioneren
quiz.answerClicked = false; // voorkom dubbel klikken op antwoord
if (counter < quiz.quizContent.length) { // test op aantal vragen
myQuestion.innerHTML = quiz.quizContent[counter].question; // laat vraag zien
myAnswer.innerHTML = "";
// zet de multiple choice antwoorden neer
for (let i = 0; i < quiz.quizContent[counter].answers.length; i++) {
let answer = document.createElement('li');
answer.className = "answer";
answer.score = quiz.quizContent[counter].answers[i].feedback;
answer.innerHTML = quiz.quizContent[counter].answers[i].answer;
myAnswer.appendChild(answer);
answer.addEventListener('click', evaluate, true)
}
}
else
{
finishQuiz(); // sluit de quiz af
}
}
function evaluate(evt) {
// console.log(evt.target); // debug
if(!quiz.answerClicked){
if (evt.target.score) {
evt.target.className = "right";
playerData.goodAnswers += 1; // increase good score
console.log("correct answer");
} else {
evt.target.className = "wrong";
playerData.wrongAnswers += 1; // increase wrong score
console.log("wrong answer");
}
quiz.answerClicked=true; // prevent double click
}
counter++;
questionBox.className = "questionBox"; // voorbereiden animatie
setTimeout(prepareQuestions, delayTime); // wacht 2 seconden voor nieuwe vraag
}
function finishQuiz() {
// afsluiting quiz geef feedback
questionBox.style.display = "none";
resultBox.style.display = "block";
quizWrapper.style.background = "silver";
resultBox.innerHTML = "<h2>Jouw resultaat <br>goede antwoorden " + playerData.goodAnswers + "<br>foute antwoorden " + playerData.wrongAnswers + "</h2>";
}
init(); // start it