-
Notifications
You must be signed in to change notification settings - Fork 0
/
zodiac.js
1379 lines (1192 loc) · 36.2 KB
/
zodiac.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*eslint-env browser */
const ASCII_ENTER = 13;
/**
* The user will enter in the English word of the corresponding flashcard.
*/
const TYPE_FLASHCARD = 0;
/**
* The user will fill in one missing character from the English word(s).
*/
const TYPE_ALPHABET = TYPE_FLASHCARD + 1;
/**
* The user will simply read the answer.
*/
const TYPE_READING = TYPE_ALPHABET + 1;
/**
* The user will have to select two matching cards.
*/
const TYPE_MATCHING = TYPE_READING + 1;
/**
* The user will select the correct definition given three choices.
*/
const TYPE_CHOOSE = TYPE_MATCHING + 1;
/**
* The user will select a card given its corresponding foreign definition.
*/
const TYPE_KARUTA = TYPE_CHOOSE + 1;
/**
* The type of flashcard run that has been selected.
*
* @see TYPE_FLASHCARD
* @see TYPE_ALPHABET
* @see TYPE_READING
* @see TYPE_MATCHING
* @see TYPE_CHOOSE
* @see TYPE_KARUTA
*/
var type = -1;
/**
* The array of English words on the flashcards.
*
* @see japanese
*/
var english = null;
/**
* The array of Japanese words on the flashcards.
*
* @see english
*/
var japanese = null;
/**
* The array of original English words on the flashcards.
*
* @see english
*/
var originalEnglish = null;
/**
* The array of original Japanese words on the flashcards.
*
* @see japanese
*/
var originalJapanese = null;
/**
* The number of cards that are left in the deck.
*/
var remaining = -1;
var idx = -1;
var files = null;
/**
* A counter for recording which file out of the files the user has
* selected is currently being processed.
*/
var fileCounter = 0;
/**
* The total number of cards in the deck.
*/
var wordCounter = 0;
/**
* The name of the sample file that's been selected. Will be null if
* the user has chosen to use a local file.
*/
var sampleFileName = null;
var front = true;
/**
* The number of characters to remove when the user is trying to fill in the blanks.
*/
var remove = -1;
/**
* An array of indices of the characters that have been removed.
*
* @see skipIdx
* @see fillIndices
*/
var skipIndices = null;
/**
* The current index within the list of removed characters that the user is currently on.undefined
*
* @see skipIndices
*/
var skipIdx = 0;
/**
* An array of characters that the user has entered.
*
* @see skipIndices
*/
var fillIndices = [];
/**
* Records how many words have been matched during a matching run.
*
* @see TYPE_MATCHING
*/
var done = [];
var beepAudio = new Audio("audio/beep." + getAudioExtension());
var errorAudio = new Audio("audio/error." + getAudioExtension());
var hasWarned = false;
var elapsedTime = -1;
function playBeepAudio() {
beepAudio.play();
}
function playErrorAudio() {
errorAudio.play();
}
function getAudioExtension() {
var video = document.createElement("audio");
return video.canPlayType("audio/ogg") ? "ogg" : "mp3";
}
function handleSampleSelect(fileName) {
sampleFileName = fileName;
files = null;
document.getElementById("startBtn").disabled = false;
}
/**
* Read the sample file and prompt its contents as a flashcard.
*/
function readSample() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
wordCounter = 0;
english = [];
japanese = [];
var text = xhttp.responseText;
var strings = text.split("\n");
for (var i = 0; i < strings.length / 2; i++) {
english[wordCounter] = strings[i * 2].trim();
japanese[wordCounter] = strings[(i * 2) + 1].trim();
done[wordCounter] = false;
wordCounter++;
}
// clone the word list
originalEnglish = english.slice(0);
originalJapanese = japanese.slice(0);
remaining = english.length;
document.getElementById("report").style.display = "none";
document.getElementById("input").value = "";
startCountdown();
}
};
xhttp.open("GET", "words/" + sampleFileName, true);
xhttp.send();
}
function handleFileSelect(e) {
if (e.target.files.length === 0) {
// the user has cancelled the dialog, disable the start button
document.getElementById("startBtn").disabled = true;
} else {
files = e.target.files;
sampleFileName = null;
document.getElementById("startBtn").disabled = false;
}
}
/**
* Reads the files that the user has selected locally and prompts them
* as flashcards.
*/
function readFiles() {
wordCounter = 0;
fileCounter = 0;
english = [];
japanese = [];
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
var strings = text.split("\n");
for (var i = 0; i < strings.length / 2; i++) {
english[wordCounter] = strings[i * 2].trim();
japanese[wordCounter] = strings[(i * 2) + 1].trim();
done[wordCounter] = false;
wordCounter++;
}
fileCounter++;
if (fileCounter !== files.length) {
reader.readAsText(files[fileCounter], "UTF-8");
} else {
// clone the word list
originalEnglish = english.slice(0);
originalJapanese = japanese.slice(0);
remaining = english.length;
document.getElementById("report").style.display = "none";
document.getElementById("input").value = "";
startCountdown();
}
};
reader.readAsText(files[fileCounter], "UTF-8");
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
function getText() {
switch (type) {
case TYPE_READING:
case TYPE_CHOOSE:
return document.getElementById("cardEnglish").checked ? english[idx] : japanese[idx];
default:
return japanese[idx];
}
}
function getAnswer() {
switch (type) {
case TYPE_READING:
case TYPE_CHOOSE:
return document.getElementById("cardEnglish").checked ? japanese[idx] : english[idx];
case TYPE_KARUTA:
return document.getElementById("cardEnglish").checked ? english[idx] : japanese[idx];
default:
return english[idx];
}
}
function updateFlashCard() {
var text = getText();
if (front) {
document.getElementById("frontContent").innerHTML = text;
document.getElementById("frontTable").style.visibility = "visible";
document.getElementById("backTable").style.visibility = "hidden";
} else {
document.getElementById("frontTable").style.visibility = "hidden";
document.getElementById("backTable").style.visibility = "visible";
document.getElementById("backContent").innerHTML = text;
}
}
/**
* Checks whether the given character code corresponds to an English character.
*
* @param charCode the character code to check
*/
function isEnglishLetter(charCode) {
return (65 <= charCode && charCode <= 90) || (97 <= charCode && charCode <= 122);
}
/**
* Retrieves an array of indices that are to be skipped given the number of characters to remove.
*
* @param remove the number of characters to remove from the current English word
*/
function getSkippedIndices(remove) {
var indices = [];
var count = 0;
for (var i = 0; i < english[idx].length; i++) {
if (isEnglishLetter(english[idx].charCodeAt(i))) {
count++;
}
}
var j = 0;
if (count <= remove) {
// will be removing all the English characters
for (i = 0; i < english[idx].length; i++) {
if (isEnglishLetter(english[idx].charCodeAt(i))) {
indices[j] = i;
j++;
}
}
return indices;
}
while (remove > 0) {
var randomIdx = Math.floor(Math.random() * english[idx].length);
while (!isEnglishLetter(english[idx].charCodeAt(randomIdx)) || indices.indexOf(randomIdx) !== -1) {
randomIdx = Math.floor(Math.random() * english[idx].length);
}
indices[j] = randomIdx;
j++;
remove--;
}
indices.sort();
return indices;
}
/**
* Returns the English word with a subset of its characters missing and replaced with an underscore.
*/
function getRemovalDisplayText() {
var display = "";
skipIndices = getSkippedIndices(remove);
for (var i = 0; i < skipIndices.length; i++) {
fillIndices[i] = -1;
}
var j = 0;
for (i = 0; i < english[idx].length; i++) {
if (i === skipIndices[j]) {
display = display + "_";
j++;
} else {
display = display + english[idx].charAt(i);
}
}
return display;
}
function show() {
idx = Math.floor(Math.random() * remaining);
updateFlashCard();
updateRemaining();
if (type === TYPE_FLASHCARD) {
document.getElementById("input").value = "";
} else if (type === TYPE_ALPHABET) {
var text = getRemovalDisplayText();
document.getElementById("alphabetsDisplay").innerHTML = text;
document.getElementById("alphabetsInput").innerHTML = text;
}
}
function updateRemaining() {
document.getElementById("remaining").innerHTML = "残り: " + remaining + "/" + wordCounter;
}
/**
* Displays a text to the user indicating that there is a mistake in the answer.
*/
function showError() {
document.getElementById("mistake").innerHTML = "<font color=\"red\">間違った!</font>";
playErrorAudio();
}
function hideError() {
document.getElementById("mistake").innerHTML = "";
}
/**
* Returns true if the given answer is correct.
*
* @param answer the answer that the user has provided
* @return true if the given answer is correct, false otherwise
*/
function check(answer) {
switch (type) {
case TYPE_READING:
return true;
case TYPE_CHOOSE:
return document.getElementById("cardEnglish").checked ? answer === japanese[idx] : answer === english[idx];
default:
return answer === english[idx];
}
}
/**
* Returns true and moves on to the next flashcard if the given answer is correct.
*
* @param answer the answer that the user has provided
* @return true if the given answer is correct, false otherwise
*/
function next(answer) {
// check if the answer matches, or if we're just doing a read through
if (check(answer)) {
remaining--;
if (type !== TYPE_READING) {
// no point in playing the sound if we're just reading the cards
playBeepAudio();
}
english[idx] = english[remaining];
japanese[idx] = japanese[remaining];
switch (type) {
case TYPE_FLASHCARD:
case TYPE_ALPHABET:
case TYPE_CHOOSE:
hideError();
break;
case TYPE_READING:
document.getElementById("readingDisplay").innerHTML = "";
break;
}
if (remaining === 0) {
showReport();
} else {
front = !front;
show();
var card = document.getElementById("card");
if (card.className === "card") {
card.className = "card flipped";
} else {
card.className = "card";
}
}
return true;
}
showError();
return false;
}
function onClick() {
var input = document.getElementById("input");
if (input.disabled) {
return;
}
next(input.value);
}
/**
* Given the current index of where the user is in completing the English word,
* either fill in the character or remove the most recently filled in character.
*
* @param c the character to enter in, or null if the most recently filled in
* character should be removed
*/
function fillInTheBlank(c) {
// has the user completed the word
var done = true;
// are there any errors
var hasErrors = false;
if (c === null) {
// remove the last character
skipIdx--;
fillIndices[skipIdx] = -1;
} else {
// fill in the current character
fillIndices[skipIdx] = c;
skipIdx++;
}
var html = "";
var j = 0;
for (var i = 0; i < english[idx].length; i++) {
var char = english[idx].charAt(i);
// is this an index that was skipped
if (i === skipIndices[j]) {
if (fillIndices[j] === char) {
// the character matches, make it green
html = html + "<font color=\"#00dd00\">" + fillIndices[j] + "</font>";
} else if (fillIndices[j] === -1) {
// this character hasn't been entered in yet, put in an underscore
html = html + "_";
done = false;
} else {
// the character is wrong, make it red
html = html + "<font color=\"#ff0000\">" + fillIndices[j] + "</font>";
done = false;
hasErrors = true;
}
j++;
} else {
html = html + char;
}
}
if (done) {
// we're done, reset and move on
skipIdx = 0;
next(english[idx]);
} else {
// update the input
document.getElementById("alphabetsInput").innerHTML = html;
if (hasErrors) {
// show errors if there are any
showError();
} else {
// clear the error message otherwise
hideError();
}
}
}
function onBackspace() {
if (type === TYPE_ALPHABET) {
if (skipIdx > 0) {
fillInTheBlank(null);
}
return true;
}
return false;
}
function onCharacter(c) {
// if the user just keeps typing, don't bother processing the extra stuff
if (type === TYPE_ALPHABET && skipIdx < skipIndices.length) {
fillInTheBlank(c);
}
}
function onShow() {
if (type === TYPE_READING) {
document.getElementById("readingDisplay").innerHTML = getAnswer();
}
}
var countdownTime = 5;
var remainingTime = -1;
/**
* The desired time limit for a time trial. Will be -1 if the run is not a time trial.
*/
var timeLimit = -1;
/**
* The time when the user started the run.
*/
var startTime = -1;
var start = -1;
function getType() {
var navbar = document.getElementById('navbar').getElementsByTagName('li');
for (var i = 0; i < navbar.length; i ++) {
var anchor = navbar[i].getElementsByTagName("a")[0];
if (anchor.hasAttribute("class")) {
var id = anchor.getAttribute("id");
if (id === "flashcard") {
return TYPE_FLASHCARD;
} else if (id === "alphabet") {
return TYPE_ALPHABET;
} else if (id === "reading") {
return TYPE_READING;
} else if (id === "matching") {
return TYPE_MATCHING;
} else if (id === "choose") {
return TYPE_CHOOSE;
} else if (id === "karuta") {
return TYPE_KARUTA;
}
throw new Error("Unknown type: " + id);
}
}
throw new Error("No types found");
}
function startCountdown() {
document.getElementById("restartBtn").disabled = false;
type = getType();
// reset all necessary values
reset();
hasWarned = false;
var showKeyboard = document.getElementById("keyboardCheckbox").checked;
switch (type) {
case TYPE_FLASHCARD:
document.getElementById("flashcardDiv").style.display = "block";
document.getElementById("flashcardContent").style.display = "inline";
document.getElementById("alphabetsContent").style.display = "none";
document.getElementById("readingContent").style.display = "none";
document.getElementById("matchingContent").style.display = "none";
document.getElementById("chooseContent").style.display = "none";
document.getElementById("karutaContent").style.display = "none";
break;
case TYPE_ALPHABET:
remove = parseInt(document.getElementById("alphabetRemoval").value, 10);
document.getElementById("flashcardDiv").style.display = "block";
document.getElementById("flashcardContent").style.display = "none";
document.getElementById("alphabetsContent").style.display = "inline";
document.getElementById("readingContent").style.display = "none";
document.getElementById("matchingContent").style.display = "none";
document.getElementById("chooseContent").style.display = "none";
document.getElementById("karutaContent").style.display = "none";
break;
case TYPE_READING:
document.getElementById("flashcardDiv").style.display = "block";
document.getElementById("flashcardContent").style.display = "none";
document.getElementById("alphabetsContent").style.display = "none";
document.getElementById("readingContent").style.display = "inline";
document.getElementById("matchingContent").style.display = "none";
document.getElementById("chooseContent").style.display = "none";
document.getElementById("karutaContent").style.display = "none";
showKeyboard = false;
break;
case TYPE_MATCHING:
document.getElementById("flashcardDiv").style.display = "none";
document.getElementById("flashcardContent").style.display = "none";
document.getElementById("alphabetsContent").style.display = "none";
document.getElementById("readingContent").style.display = "none";
document.getElementById("matchingContent").style.display = "inline";
document.getElementById("chooseContent").style.display = "none";
document.getElementById("karutaContent").style.display = "none";
showKeyboard = false;
break;
case TYPE_CHOOSE:
document.getElementById("flashcardDiv").style.display = "block";
document.getElementById("flashcardContent").style.display = "none";
document.getElementById("alphabetsContent").style.display = "none";
document.getElementById("readingContent").style.display = "none";
document.getElementById("matchingContent").style.display = "none";
document.getElementById("chooseContent").style.display = "inline";
document.getElementById("karutaContent").style.display = "none";
showKeyboard = false;
break;
case TYPE_KARUTA:
document.getElementById("flashcardDiv").style.display = "none";
document.getElementById("flashcardContent").style.display = "none";
document.getElementById("alphabetsContent").style.display = "none";
document.getElementById("readingContent").style.display = "none";
document.getElementById("matchingContent").style.display = "inline";
document.getElementById("chooseContent").style.display = "none";
document.getElementById("karutaContent").style.display = "inline";
showKeyboard = false;
break;
}
if (showKeyboard) {
document.getElementById("canvas").style.display = "inline";
} else {
document.getElementById("canvas").style.display = "none";
}
if (type === TYPE_MATCHING) {
fill();
updateRemaining();
} else if (type === TYPE_KARUTA) {
resetMatchingCards();
updateKaruta();
updateRemaining();
} else {
show();
if (type === TYPE_CHOOSE) {
makeSelections();
}
}
// hide the setup popup
document.getElementById("openModal").style.display = "none";
updateFlashCard();
if (document.getElementById("timeCheckbox").checked) {
timeLimit = parseInt(document.getElementById("time").value, 10);
remainingTime = timeLimit;
// show the countdown timer
document.getElementById("countdown").style.display = "inline";
start = new Date().getTime();
drawCountdown();
} else {
timeLimit = -1;
remainingTime = -1;
// not a time trial, remove the trial timer
document.getElementById("trialTimer").style.display = "none";
// show the content
document.getElementById("body").style.background = "#eeeeee";
document.getElementById("content").style.display = "inline";
// enable the input field and grant it focus
document.getElementById("input").disabled = false;
document.getElementById("input").focus();
startTime = new Date().getTime();
}
}
/**
* Resets the state of the application in preparation of the next run.
*/
function reset() {
canvas = document.getElementById("countdownCanvas");
ctx = canvas.getContext("2d");
// remove the red glow around the input field
document.getElementById("input").className = "input";
hideError();
document.getElementById("chooseBackContent1").innerHTML = "";
document.getElementById("chooseBackContent2").innerHTML = "";
document.getElementById("chooseBackContent3").innerHTML = "";
document.getElementById("chooseFrontContent1").innerHTML = "";
document.getElementById("chooseFrontContent2").innerHTML = "";
document.getElementById("chooseFrontContent3").innerHTML = "";
}
function getOption(answer, option) {
var options = document.getElementById("cardEnglish").checked ? originalJapanese : originalEnglish;
var value = null;
while (true) {
var optIdx = Math.floor(Math.random() * options.length);
if (option !== options[optIdx] && answer !== options[optIdx]) {
return options[optIdx];
}
}
}
function makeSelections() {
var contents = [];
for (var i = 1; i < 4; i++) {
contents[i - 1] = document.getElementById(back ? "chooseBackContent" + i : "chooseFrontContent" + i);
}
var options = [];
var answer = getAnswer();
options[0] = getOption(answer, answer);
options[1] = getOption(answer, options[0]);
var answerIdx = Math.floor(Math.random() * 3);
var j = 0;
for (var i = 0; i < 3; i++) {
if (answerIdx === i) {
contents[i].innerHTML = answer;
} else {
contents[i].innerHTML = options[j++];
}
}
}
var jp = -1;
function isDone(array) {
for (var i = 0; i < array.length; i++) {
if (!array[i]) {
return false;
}
}
return true;
}
/**
* Updates the table of cards with an assortment of words from the word
* list and then returns the number of cards on the screen.
*
* @return the number of cards that can be selected by the user
*/
function updateKaruta() {
var count = 0;
var max = remaining > 12 ? 12 : remaining;
var cards = [];
var selected = [];
var words = [];
for (var i = 0; i < max; i++) {
cards[i] = false;
}
for (var i = 0; i < remaining; i++) {
selected[i] = false;
}
var keyIdx = Math.floor(Math.random() * max);
var key = null;
while (!isDone(cards)) {
var c = Math.floor(Math.random() * remaining);
while (selected[c]) {
c = Math.floor(Math.random() * remaining);
}
cards[count] = true;
if (document.getElementById("cardEnglish").checked) {
if (keyIdx === count) {
key = japanese[c];
idx = c;
}
words[count++] = english[c];
} else {
if (keyIdx === count) {
key = english[c];
idx = c;
}
words[count++] = japanese[c];
}
selected[c] = true;
}
for (var i = 0; i < max; i++) {
var contentId = back ? "backContent" + (i + 1) : "frontContent" + (i + 1);
var p = document.getElementById(contentId);
p.innerHTML = words[i];
}
if (max !== 12) {
hideOtherMatchingCards(max);
}
document.getElementById("karutaContent").innerHTML = key;
return max;
}
function getMaxFill(cards) {
if (cards === 9 || cards === 10) {
return 10;
} else if (cards === 7 || cards === 8) {
return 8;
} else if (cards >= 6) {
return 12;
}
return cards * 2;
}
function fill() {
resetMatchingCards();
var count = 0;
var max = getMaxFill(english.length);
var cards = [];
for (var i = 0; i < max; i++) {
cards[i] = false;
}
while (count < 12 && !isDone(cards)) {
var matchingIdx = -1;
if (jp === -1) {
matchingIdx = Math.floor(Math.random() * wordCounter);
while (done[matchingIdx]) {
matchingIdx = Math.floor(Math.random() * wordCounter);
}
} else {
matchingIdx = jp;
}
var c = Math.floor(Math.random() * cards.length);
while (cards[c]) {
c = Math.floor(Math.random() * cards.length);
}
var p = document.getElementById("frontContent" + (c + 1));
if (jp === -1) {
p.innerHTML = english[matchingIdx];
jp = matchingIdx;
} else {
p.innerHTML = japanese[jp];
jp = -1;
}
cards[c] = true;
done[matchingIdx] = true;
count++;
}
if (cards.length < 12) {
hideOtherMatchingCards(cards.length);
}
}
function resetMatchingCards() {
for (var i = 1; i < 13; i++) {
document.getElementById("front" + i).style.display = "block";
document.getElementById("back" + i).style.display = "none";
document.getElementById("card" + i).className = "card";
document.getElementById("card" + i).style.display = "block";
document.getElementById("frontTable" + i).style.visibility = "visible";
document.getElementById("frontTable" + i).className = "";
document.getElementById("backTable" + i).style.visibility = "hidden";
document.getElementById("backTable" + i).className = "";
}
back = false;
}
function hideOtherMatchingCards(offset) {
for (var i = offset + 1; i < 13; i++) {
document.getElementById("card" + i).style.display = "none";
}
}
var back = false;
function flip() {
var count = 0;
var cards = [ false, false, false, false, false, false, false, false, false, false, false, false ];
var undone = 0;
for (var i = 0; i < done.length; i++) {
if (!done[i]) {
undone++;
}
}
var incomplete = getMaxFill(undone);
while (count < incomplete) {
var matchingIdx = -1;
if (jp === -1) {
matchingIdx = Math.floor(Math.random() * wordCounter);
while (done[matchingIdx]) {
matchingIdx = Math.floor(Math.random() * wordCounter);
}
} else {
matchingIdx = jp;
}
var c = Math.floor(Math.random() * incomplete);
while (cards[c]) {
c = Math.floor(Math.random() * incomplete);
}
content = back ? "backContent" + (c + 1) : "frontContent" + (c + 1);
p = document.getElementById(content);
if (jp === -1) {
p.innerHTML = english[matchingIdx];
jp = matchingIdx;
} else {
p.innerHTML = japanese[jp];
jp = -1;
}
cards[c] = true;
done[matchingIdx] = true;
count++;
}
flipCards(incomplete);
}
/**
* Flips the cards on the table.
*
* @param incomplete the number of cards that are on the screen
*/
function flipCards(incomplete) {
for (var i = 1; i < 13; i++) {
var card = document.getElementById("card" + i);
if (card.className === "card") {
card.className = "card flipped";
} else {
card.className = "card";
}
if (back) {
document.getElementById("front" + i).style.display = "none";
document.getElementById("back" + i).style.display = "block";
document.getElementById("frontTable" + i).style.visibility = "hidden";
document.getElementById("backTable" + i).style.visibility = "visible";
document.getElementById("backTable" + i).className = "";
} else {
document.getElementById("front" + i).style.display = "block";
document.getElementById("back" + i).style.display = "none";
document.getElementById("frontTable" + i).style.visibility = "visible";
document.getElementById("backTable" + i).style.visibility = "hidden";
document.getElementById("frontTable" + i).className = "";
}
}
if (incomplete < 12) {
hideOtherMatchingCards(incomplete);
}
}
var frontTables = [];
var backTables = [];
function mouseDownKaruta(table) {
var selection = table.children[0].children[0].children[0].innerHTML;
var answer = getAnswer();
if (selection === getAnswer()) {
remaining--;
updateRemaining();
playBeepAudio();
if (remaining === 0) {