-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (90 loc) · 2.92 KB
/
script.js
File metadata and controls
116 lines (90 loc) · 2.92 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
112
113
114
115
116
let cards;
let attributes;
let uniqueAttributes;
let currentCard;
let currentAttribute;
async function loadData() {
console.log("run")
let response = await fetch("./data.json");
let json = await response.json();
attributes = json.attributes;
cards = json.cards;
//Check that all objects has all neccessary attributes
outerLoop:
for (const card of cards) {
for (const attribute of attributes) {
if(!card[attribute]) {
alert(`Not all objects have the required attributes: ${attribute}`);
break outerLoop;
}
}
}
//Find all unique values for each attribute
uniqueAttributes = {};
for (const attribute of attributes) {
const uniques = new Set();
for (const card of cards) {
uniques.add(card[attribute]);
}
uniqueAttributes[attribute] = Array.from(uniques);
}
displayNextCard();
}
function buttonPress(buttonIndex) {
let button = document.getElementById(`button${buttonIndex}`);
let answer = button.innerHTML;
if(answer === cards[currentCard][currentAttribute]) {
button.style.background = "green";
setTimeout(displayNextCard, 500);
}
else {
button.style.background = "red";
}
}
function displayNextCard() {
let cardIndex = random(0, cards.length);
let attribIndex = random(0, attributes.length);
currentCard = cardIndex;
currentAttribute = attributes[attribIndex];
displayCard(cardIndex, attribIndex);
}
function displayCard(cardIndex, attribIndex) {
let card = cards[cardIndex];
//Image section
document.getElementById("image").src = card.image;
//Text section
let list = []
for (const i in attributes) {
let attribute = attributes[i];
let p = document.createElement("p");
if(i == attribIndex)
p.innerHTML = `${attribute}: ???`;
else
p.innerHTML = `${attribute}: ${card[attribute]}`
list.push(p);
}
document.getElementById("text_section").replaceChildren(...list);
//Button section
let options = createOptions(cardIndex, attribIndex);
for(let i = 0; i < 3; i++) {
let button = document.getElementById(`button${i}`);
button.innerHTML = options[i];
button.style.background = "";
}
}
function createOptions(dataIndex, attribIndex) {
let options = [];
let attribute = attributes[attribIndex];
//TODO: Better picking algo
options.push(cards[dataIndex][attribute]);
options.push(uniqueAttributes[attribute][random(0, uniqueAttributes[attribute].length)]);
options.push(uniqueAttributes[attribute][random(0, uniqueAttributes[attribute].length)]);
//Mini shuffle
for(let i = random(0, 3); i > 0; i--) {
options.push(options.shift());
}
return options;
}
function random(start, end) {
return start + Math.floor(Math.random() * (end-start))
}