-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
206 lines (179 loc) · 6.08 KB
/
index.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<html>
<head>
<title>Flash Cards</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
p, span {
color: white;
text-align: center;
font-size: 70px;
line-height: inherit;
}
#answer p {
font-size: 30px;
line-height: 15px;
}
#question, #answer {
display: block;
position: fixed;
top: 50%;
left: 50%;
right: 0px;
width: 100%;
height: 300px;
margin-top: -150px;
margin-left: -50%;
vertical-align: middle;
line-height: 150px;
}
#answer {
margin-top: -90px;
}
</style>
<script type="text/javascript" src="cards_new.js"></script>
</head>
<body>
<div id="question">
<p id="question1"><!-- Contents added by Javascript --></p>
</div>
<div id="answer">
<p id="answer1"><!-- Contents added by Javascript --></p>
<p id="answer2"><!-- Contents added by Javascript --></p>
<p id="answer3"><!-- Contents added by Javascript --></p>
</div>
</body>
<script type="text/javascript">
var STATE = 'QUESTION';
var CURRENT_CARD = 0;
var $$ = function $$(id){
return document.getElementById(id);
}
var toggleCards = function toggleCards(){
STATE === 'QUESTION' || STATE === 'QUESTION_NUMBER' ? revealAnswer() : chooseCard();
}
var chooseCard = function chooseCard(){
$$('question').style.display = 'block'
$$('answer').style.display = 'none'
document.body.style.background = '#111111'
if(Math.random() > 0.9) {
return chooseNumberCard();
}
STATE = 'QUESTION';
CURRENT_CARD = getRandomCardIndex();
var startLanguage = chooseStartLanguage();
$$('question1').innerHTML = getAnyPossibleValue(CARDS[CURRENT_CARD][startLanguage]);
}
var chooseNumberCard = function chooseNumberCard() {
STATE = 'QUESTION_NUMBER';
CURRENT_CARD = '' + Math.floor(Math.random() * 10000)
$$('question1').innerHTML = convertNumberToFarsi(CURRENT_CARD);
}
var chooseStartLanguage = function chooseStartLanguage(){
var possibleLanguages = [0, 2];
return possibleLanguages[Math.floor(Math.random() * possibleLanguages.length)];
}
var convertNumberToFarsi = function convertNumberToFarsi(numberString) {
if(numberString === '') {
return '';
}
var numToConvert = numberString.charAt(0);
return romanNumberToFarsi[numToConvert] + convertNumberToFarsi(numberString.substring(1,numberString.length));
}
var romanNumberToFarsi = {
'0' : '۰',
'1' : '۱',
'2' : '۲',
'3' : '۳',
'4' : '۴',
'5' : '۵',
'6' : '۶',
'7' : '۷',
'8' : '۸',
'9' : '۹',
};
var revealAnswer = function revealAnswer(){
$$('question').style.display = 'none'
$$('answer').style.display = 'block'
document.body.style.background = '#123424'
if(STATE === 'QUESTION_NUMBER') {
return revealNumberAnswer();
}
STATE = 'ANSWER'
$$('answer1').innerHTML = getAllPossibleValues(CARDS[CURRENT_CARD][0])
$$('answer2').innerHTML = getAllPossibleValues(CARDS[CURRENT_CARD][1])
$$('answer3').innerHTML = getAllPossibleValues(CARDS[CURRENT_CARD][2])
}
var revealNumberAnswer = function revealNumberAnswer() {
STATE = 'ANSWER_NUMBER'
$$('answer1').innerHTML = '' + CURRENT_CARD;
$$('answer2').innerHTML = '';
$$('answer3').innerHTML = '' + CURRENT_CARD;
}
var getAllPossibleValues = function getAllPossibleValues(elementOrArray){
if (typeof elementOrArray === 'string') {
return elementOrArray;
} else {
return '[' + elementOrArray.join(' / ') + ']';
}
}
var getAnyPossibleValue = function getAnyPossibleValue(elementOrArray){
if (typeof elementOrArray === 'string') {
return elementOrArray;
} else {
var choice = Math.floor(Math.random() * elementOrArray.length);
return elementOrArray[choice];
}
}
var populateRandomDistribution = function populateRandomDistribution() {
var returnVal = [];
var numEntries = 1;
for(var currentCardIndex = 0; currentCardIndex < CARDS.length; currentCardIndex++) {
if(CARDS.length - currentCardIndex < 200) {
numEntries += 1;
}
for(var entryNum = 0; entryNum < numEntries; entryNum++) {
returnVal.push(currentCardIndex);
}
}
return returnVal;
};
var getRandomCardIndex = function getRandomCardIndex() {
// return randomDistribution[Math.floor(Math.random() * randomDistribution.length)];
return randomDistribution[Math.floor(Math.random() * 50)];
};
var currentIndex = -1;
var getOrderedCardIndex = function getOrderedCardIndex() {
currentIndex++;
// currentIndex = currentIndex % CARDS.length;
currentIndex = currentIndex % 50;
return currentIndex;
};
/* ----------- UNIT TESTS ----------- */
var itShouldSelectEveryValueEventually = function itShouldSelectEveryValueEventually() {
var allOptions = [];
for(var x = 0; x < CARDS.length; x++){ allOptions.push(0); }
for(var i = 0; i < 10000000; i++){
allOptions[getRandomCardIndex()] += 1;
}
var missedIndices = [];
for(var i = 0; i < allOptions.length; i++){
if(allOptions[i] == 0){ missedIndices.push(i); }
}
alert(missedIndices.length + " missed cards out of " + allOptions.length);
console.log(missedIndices);
console.log(allOptions);
}
var randomDistribution = populateRandomDistribution();
var checkLoad = function() {
if($$('answer3')){
document.body.onclick = toggleCards;
document.body.ontouchend = toggleCards;
window.addEventListener("keyup", toggleCards, false);
chooseCard();
alert("Total Cards: " + CARDS.length);
} else {
setTimeout(checkLoad, 11);
}
}();
</script>
</html>