-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetflashcard.js
70 lines (68 loc) · 2.07 KB
/
getflashcard.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
function FlashDeck(key) {
var deckList = key["deck"],
deck = [],
discard = [];
self.shuffle = function(deckOnly) {
var tmp = [];
while(deck.length > 0) {
tmp.push(deck.pop());
}
while(discard.length > 0) {
tmp.push(discard.pop());
}
while(tmp.length > 0) {
deck.splice(Math.floor(Math.random() * (deck.length + 1)), 0, tmp.pop());
}
}
self.draw = function() {
if(deck.length < 1) {
self.shuffle();
}
var card = deck.pop();
discard.push(card);
return card;
}
for(var c in deckList) {
deck.push(deckList[c]);
}
return self;
}
var unixDeck;
function nextCard() {
var card = unixDeck.draw();
var answerElem = document.getElementById("answer");
var promptElem = document.getElementById("prompt");
answerElem.hidden = true;
promptElem.innerHTML = card["prompt"];
answerElem.innerHTML = card["answer"];
}
function showAnswer() {
document.getElementById("answer").hidden = false;
}
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'answerkey.json', true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
function init() {
loadJSON(function(response) {
var answerkey = JSON.parse(response);
unixDeck = new FlashDeck(answerkey);
unixDeck.shuffle();
var show = document.getElementById("showanswer");
var next = document.getElementById("next");
show.disabled = false;
next.disabled = false;
nextCard();
show.addEventListener('click', showAnswer);
next.addEventListener('click', nextCard);
});
}
init();