-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathreview_game_script.js
More file actions
111 lines (100 loc) · 2.99 KB
/
review_game_script.js
File metadata and controls
111 lines (100 loc) · 2.99 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// version 2020.02.10
var questions;
var currentQuestion = 0;
var randomSeed = "";
var numberOfOptions = 6;
document.body.style.backgroundColor = "#343a40";
document.getElementById("windowLocation").innerHTML = window.location;
if (localStorage.getItem("randomseed") === null) {
randomSeed = getRandomSeed();
localStorage.setItem("currentquestion", currentQuestion);
} else {
randomSeed = localStorage.getItem("randomseed");
currentQuestion = JSON.parse(localStorage.getItem("currentquestion"));
}
$.getJSON("./trivia.json", function(json) {
questions = shuffle(json, randomSeed);
//console.log(json);
loadQuestion();
});
function loadQuestion() {
document.getElementById("questionText").innerHTML =
questions[currentQuestion % questions.length].question;
loadAnswers();
}
function loadAnswers() {
Math.seedrandom(randomSeed);
var answers = [questions[currentQuestion % questions.length].answer];
while (answers.length < numberOfOptions) {
var flag = false;
Math.seedrandom("" + new Date().getMilliseconds());
var newAnswer =
questions[Math.floor(Math.random() * questions.length)].answer;
for (var i = 0; i < answers.length; i++) {
if (answers[i].toLowerCase() == newAnswer.toLowerCase()) {
flag = true;
break;
}
}
if (!flag) {
answers.push(newAnswer);
}
}
console.log(answers);
var answers2 = answers.slice(0);
answers2 = shuffle(answers2, "" + new Date().getMilliseconds());
updateOptions(answers2);
}
function updateOptions(answers) {
console.log(answers);
var optArray = document.getElementsByClassName("opt");
//console.log(optArray);
for (var i = 0; i < answers.length; i++) {
optArray[i].value = answers[i];
optArray[i].innerHTML = answers[i];
}
document.getElementById("pick").selected = true;
}
function checkAnswer(value) {
if (
value.toLowerCase() ==
questions[currentQuestion % questions.length].answer.toLowerCase()
) {
document.body.style.backgroundColor = "#00ff00";
setTimeout(() => {
document.body.style.backgroundColor = "#343a40";
}, 200);
currentQuestion++;
localStorage.setItem("currentquestion", currentQuestion);
loadQuestion();
} else {
document.body.style.backgroundColor = "#ff0000";
setTimeout(() => {
document.body.style.backgroundColor = "#343a40";
}, 200);
}
}
function getRandomSeed() {
var r = "" + new Date().getMilliseconds();
localStorage.setItem("randomseed", r);
Math.seedrandom(r);
return r;
}
function shuffle(array, myseed) {
var temporaryValue,
randomIndex;
Math.seedrandom(myseed);
for(var i=0;i<array.length;i++){
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * array.length);
// And swap it with the current element.
temporaryValue = array[i];
array[i] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function compareArrays(a, b) {
if (JSON.stringify(a) == JSON.stringify(b)) return true;
else return false;
}