-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
165 lines (135 loc) · 3.18 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var billedtitle = "Jagtbilleder";
var counter = 0;
var count = 0;
var highscore = 0;
var x = 0;
var y = 0;
var gruppe = "";
var art = "";
var local = [];
var b = false;
var obj = JSON.parse(data);
function generateVariables(){
x = Math.floor(Math.random() * obj.Animals.length); //hvilken index of dyr
y = Math.floor(Math.random() * obj.Animals[x].Links.length); //hvilket index of link på dyre objektet
gruppe = obj.Animals[x].Gruppe;
art = obj.Animals[x].Art;
}
//slet billede
function sletBillede(){
obj.Animals[x].Links.pop();
generateVariables();
findLikewise();
changeImage();
setOptions();
changePictureText();
};
//check
function check(){
if(b==false){
generateVariables();
findLikewise();
changeImage();
setOptions();
changePictureText();
b = true;
} else {
submitAnswer();
}
}
function submitAnswer() {
var radios = document.getElementsByName('choice');
var val= "";
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
val = radios[i].value;
break;
}
}
if (val == "" ) {
alert('Fill in');
} else if (val == art) {
alert('Korrekt!');
count++;
if(count > highscore){
highscore = count;
}
changeCounter();
generateVariables();
findLikewise();
changeImage();
setOptions();
changePictureText();
} else {
count = 0;
changeCounter();
alert('Forkert');
}
};
function changeImage(){
var img = document.getElementById("img");
img.src = obj.Animals[x].Links[y];
};
function findLikewise(){
local = [];
for (var i = 0; i < obj.Animals.length; i++) {
if(obj.Animals[i].Gruppe == gruppe && i != x){
console.log(obj.Animals[i].Gruppe);
console.log(gruppe);
local.push(i);
}
}
}
function setOptions(){
//random fra local
var a = Math.floor(Math.random() * local.length);
a = local[a];
local.splice(a,a);
var b = Math.floor(Math.random() * local.length);
b = local[b];
//arterne fra random
var a_art = obj.Animals[a].Art;
var b_art = obj.Animals[b].Art;
var c_art = art;
//svar array
var answers = [];
answers.push(a_art);
answers.push(c_art);
while(answers.length != 3){
if(answers.includes(b_art)){
b = Math.floor(Math.random() * local.length);
b = local[b];
b_art = obj.Animals[b].Art;
} else {
answers.push(b_art);
}
}
shuffle(answers);
var html = '<form>';
for (var i = 0, length = answers.length; i < length; i++) {
var s = answers[i];
html += '<input type="radio" onclick="submitAnswer()" name="choice" value="'+ s +'">' + '<label for="'+s+'">'+s+'</label>';
}
html += '</form>';
document.getElementById('log').innerHTML = html;
}
function changePictureText(){
var subtitle = document.getElementById("billedtitle");
subtitle.innerHTML = gruppe;
}
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
}
function changeCounter(){
var cont = document.getElementById("counter");
cont.innerHTML = "score: " + count + '<br>' +"highscore: " + highscore;
}
function exportJSON(){
console.log("data = '" + JSON.stringify(obj) + "'")
};