-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
925 lines (754 loc) · 35.3 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
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
//.----------------. .-----------------. .----------------. .-----------------. .----------------. .----------------. .----------------. .----------------.
//| .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. || .--------------. |
//| | ____ ____ | || | ____ _____ | || | ______ | || | ____ _____ | || | _________ | || | _____ _____ | || | _____ _____ | || | _____ _____ | |
//| ||_ _| |_ _| | || ||_ \|_ _| | || | .' ___ | | || ||_ \|_ _| | || | | _ _ | | || ||_ _||_ _|| || ||_ _||_ _|| || ||_ _||_ _|| |
//| | \ \ / / | || | | \ | | | || | / .' \_| | || | | \ | | | || | |_/ | | \_| | || | | | /\ | | | || | | | /\ | | | || | | | /\ | | | |
//| | \ \ / / | || | | |\ \| | | || | | | | || | | |\ \| | | || | | | | || | | |/ \| | | || | | |/ \| | | || | | |/ \| | | |
//| | \ ' / | || | _| |_\ |_ | || | \ `.___.'\ | || | _| |_\ |_ | || | _| |_ | || | | /\ | | || | | /\ | | || | | /\ | | |
//| | \_/ | || ||_____|\____| | || | `._____.' | || ||_____|\____| | || | |_____| | || | |__/ \__| | || | |__/ \__| | || | |__/ \__| | |
//| | | || | | || | | || | | || | | || | | || | | || | | |
//| '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' || '--------------' |
// '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------' '----------------'
//
// Coded by vncntwww :)
// This is a simple web app that allows you to create and study flashcards.
// The app is written in HTML, CSS and JavaScript.
// Source code was last modified on 2024-02-02
//const serverURL = "https://YOURSERVER/server/"; //Modify this to your server URL
var allowPartialCredit = true;
var accuracyThreshold = 0.75;
document.getElementById('createButton').addEventListener('click', openCreatePopup);
document.getElementById('importButton').addEventListener('click', openPopup);
document.getElementById('settingsBtn').addEventListener('click', openSettingsModal);
document.getElementById('importFileBtn').addEventListener('change', function (event) {
const fileInput = event.target;
// Check if a file is selected and has the correct extension
if (fileInput.files.length > 0 && fileInput.files[0].name.endsWith('.wvocab')) {
readFile(fileInput.files[0]);
} else if (fileInput.files.length > 0 && fileInput.files[0].name.endsWith('.txt')) {
alert('This file might not be supported correctly. It will still be loaded, but this may change in the future. Please use .wvocab files instead.');
readFile(fileInput.files[0]);
} else {
//alert('Please select a valid .wvocab file.');
broadcast("Please select a valid .wvocab file.", "Okay", "", closeBroadcast, closeBroadcast)
// Optionally clear the file input
fileInput.value = '';
}
});
///////////////
//NAMING&VARS//
///////////////
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('versionNumber').innerHTML = applicationVersion;
if(disableServer) {
document.getElementById('chooseFromBCN').style.display = 'none';
}
});
//////////
//POPUPS//
//////////
function openPopup() {
document.getElementById('popup').style.display = 'block';
}
function closePopup() {
document.getElementById('popup').style.display = 'none';
}
function openCreatePopup() {
document.getElementById('createPopup').style.display = 'block';
}
function closeCreatePopup() {
document.getElementById('createPopup').style.display = 'none';
}
function openSettingsModal() {
document.getElementById('settingsModal').style.display = 'block';
}
function closeSettingsModal() {
document.getElementById('settingsModal').style.display = 'none';
}
function openBCNModal() {
getVocabSets();
document.getElementById('bcnsetsModal').style.display = 'block';
}
function closeBCNModal() {
document.getElementById('bcnsetsModal').style.display = 'none';
}
function applySettings() {
allowPartialCredit = document.getElementById('partialCreditCheckbox').checked;
accuracyThreshold = parseFloat(document.getElementById('accuracyThreshold').value);
console.log('Allow Partial Credit:', allowPartialCredit);
console.log('Accuracy Threshold:', accuracyThreshold);
closeSettingsModal();
}
//////////////////////
//BROADCAST FUNCTION//
//////////////////////
function broadcast(text, button1Text, button2Text, button1Action, button2Action) {
//EXAMPLE broadcast("What do you want to do?", "Close", "Cancel", closeBroadcast, closeBroadcast)
const popup = document.getElementById('broadcastPopup');
const popupText = document.getElementById('broadcastText');
const button1 = document.getElementById('broadcastButton1');
const button2 = document.getElementById('broadcastButton2');
popup.style.display = 'block';
// Set the text and actions for the popup
popupText.innerText = text;
button1.innerText = button1Text;
button2.innerText = button2Text;
button1.onclick = button1Action;
button2.onclick = button2Action;
if(button2.innerText.length == 0) {
button2.style.visibility = "hidden"
button2.style.display = "none"
} else {
button2.style.visibility = "visible"
button2.style.display = "inline-block"
}
}
function closeBroadcast() {
const popup = document.getElementById('broadcastPopup');
popup.style.display = 'none';
}
function broadcastOK(text, buttonText, buttonAction) {
//EXAMPLE broadcastOK("This is a test.", "Okay", closeBroadcast)
const popup = document.getElementById('broadcastPopup');
const popupText = document.getElementById('broadcastText');
const button1 = document.getElementById('broadcastButton1');
const button2 = document.getElementById('broadcastButton2');
popup.style.display = 'block';
// Set the text and actions for the popup
popupText.innerText = text;
button1.innerText = buttonText;
button2.innerText = "";
button1.onclick = buttonAction;
button2.onclick = buttonAction;
if(button2.innerText.length == 0) {
button2.style.visibility = "hidden"
button2.style.display = "none"
} else {
button2.style.visibility = "visible"
button2.style.display = "inline-block"
}
// Add event listener for the enter key
document.addEventListener('keydown', enterKeyListener);
function enterKeyListener(event) {
if (event.key === 'Enter') {
event.preventDefault();
console.log("enterKeyListener fired: line 160")
buttonAction();
document.removeEventListener('keydown', enterKeyListener); // Remove the listener after handling the event
}
}
}
//////INIT//////
console.log('%c TurboTerms ', 'background-image: linear-gradient(90deg,#2c3e50,#3498db); color: white;font-weight:900;font-size:1rem; padding:20px;');
console.log('%c Developed by Vincent Wackler ', 'background-image: linear-gradient(90deg,#ff0000,#ff873c); color: white;font-weight:900;font-size:1rem; padding:20px;');
//////////
//SERVER//
//////////
async function getVocabSets() {
try {
const response = await fetch(serverURL + 'get-vocabsets/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({}),
});
const data = await response.json();
if (response.ok) {
console.log(data);
const vocabSetsContainer = document.getElementById('vocabSetsContainer');
vocabSetsContainer.innerHTML = '';
data.directories.forEach(directory => {
// Check if the directory name is not 'index.json'
if (directory !== 'index.json') {
const vocabSetElement = document.createElement('div');
vocabSetElement.innerText = directory;
vocabSetElement.style.cursor = 'pointer';
vocabSetElement.onclick = function () {
getFilesInDirectory(directory);
console.log('Clicked on directory:', directory);
};
vocabSetsContainer.appendChild(vocabSetElement);
}
});
data.files.forEach(file => {
// Check if the file name is not 'index.json'
if (file !== 'index.json') {
const vocabSetElement = document.createElement('div');
vocabSetElement.innerText = file;
vocabSetElement.style.cursor = 'pointer';
vocabSetElement.onclick = function () {
closeBCNModal();
loadBCNSet(file);
};
vocabSetsContainer.appendChild(vocabSetElement);
}
});
} else {
console.error('Error:', data.error);
}
} catch (error) {
console.error('Error fetching data:', error.message);
}
}
async function getFilesInDirectory(directory) {
try {
const response = await fetch(serverURL + 'get-files-in-directory/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ directory }),
});
const data = await response.json();
if (response.ok) {
console.log(data.files);
const vocabSetsContainer = document.getElementById('vocabSetsContainer');
vocabSetsContainer.innerHTML = '';
data.files.forEach(file => {
const vocabSetElement = document.createElement('div');
vocabSetElement.innerText = file;
vocabSetElement.style.cursor = 'pointer';
vocabSetElement.onclick = function () {
closeBCNModal();
loadBCNSetWithDir(directory, file);
};
vocabSetsContainer.appendChild(vocabSetElement);
});
} else {
console.error('Error:', data.error);
}
} catch (error) {
console.error('Error fetching data:', error.message);
}
}
/////////////
//BCN-STUFF//
/////////////
function toggleGridDropdown() {
const grid_dropdown = document.getElementById('grid-dropdown');
grid_dropdown.classList.toggle('hidden');
if(grid_dropdown.style.display == "flex") {
grid_dropdown.style.display = "none";
} else {
grid_dropdown.style.display = "flex";
}
}
//////////////
//FLASHCARDS//
//////////////
let flashcards = [];
let currentCardIndex = 0;
function importVocab() {
const vocabInput = document.getElementById('vocabInput').value;
if (vocabInput) {
processInput(vocabInput);
} else {
const fileInput = document.getElementById('importFileBtn');
if (fileInput.files.length > 0 && fileInput.files[0].name.endsWith('.wvocab')) {
readFile(fileInput.files[0]);
} else if (fileInput.files.length > 0 && fileInput.files[0].name.endsWith('.txt')) {
readFile(fileInput.files[0]);
} else {
//alert('Please select a valid .wvocab file or provide a URL.');
console.log("Filename: " + fileInput.files[0].name);
broadcast("Please select a valid .wvocab file or provide a URL.", "Okay", "", closeBroadcast, closeBroadcast)
}
}
}
function addFromInternet() {
const urlInput = prompt('Enter the URL of the .wvocab file:');
if (urlInput) {
fetch(urlInput)
.then(response => response.text())
.then(content => processInput(content))
.catch(error => {
console.error('Error fetching vocab file from URL:', error);
//alert('Error fetching vocab file from URL. Please make sure the URL is correct.');
broadcast("Error fetching vocab file from URL. Please make sure the URL is correct.", "Okay", "", closeBroadcast, closeBroadcast)
});
} else {
//alert('Please select a valid .wvocab file or provide a URL.');
broadcast("Please select a valid .wvocab file or provide a URL.", "Okay", "", closeBroadcast, closeBroadcast)
}
}
function loadBCNSet(setname) {
fetch(fileStorageURL + setname)
.then(response => response.text())
.then(content => processInput(content))
.catch(error => {
console.error('Error fetching vocab file from URL:', error);
//alert('Error fetching vocab file from URL. Please make sure the URL is correct.');
broadcast("Error fetching vocab file from server. Please make sure to use our official domain.", "Okay", "", closeBroadcast, closeBroadcast)
});
}
function loadBCNSetWithDir(directory, setname) {
fetch(fileStorageURL + directory + "/" + setname)
.then(response => response.text())
.then(content => processInput(content))
.catch(error => {
console.error('Error fetching vocab file from URL:', error);
//alert('Error fetching vocab file from URL. Please make sure the URL is correct.');
broadcast("Error fetching vocab file from server. Please make sure to use our official domain.", "Okay", "", closeBroadcast, closeBroadcast)
});
}
function processInput(input) {
const pairs = input.split(';');
flashcards = pairs.map(pair => {
const [rawVocab, rawTranslation] = pair.split('#').map(str => str.trim());
console.log('[DEBUG] rawVocab:', rawVocab, 'rawTranslation:', rawTranslation);
return { vocab: rawVocab, translation: rawTranslation };
});
flashcards = shuffle(flashcards);
closePopup();
startQuiz();
}
function readFile(file) {
const reader = new FileReader();
reader.onload = function (event) {
const content = event.target.result;
processInput(content);
};
reader.readAsText(file);
}
function shuffle(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
}
let multipleChoiceMode = true;
let questioningDirection = 'AB';
function startQuiz() {
document.querySelector('.homepage').style.display = 'none';
document.getElementById('nextbtn').style.display = 'none';
document.querySelector('.quiz').style.display = 'block';
console.log('[DEBUG] Starting the quiz!');
// Find the shortest vocab
let shortestVocab = flashcards[0].vocab;
flashcards.forEach(card => {
if (card.vocab.length < shortestVocab.length) {
shortestVocab = card.vocab;
}
});
// Find the translation corresponding to the shortest vocab
const shortestTranslation = flashcards.find(card => card.vocab === shortestVocab).translation;
console.log("A -> B: " + shortestVocab + " -> " + shortestTranslation + " | B -> A: " + shortestTranslation + " -> " + shortestVocab);
broadcast("In which direction would you like to be quizzed?", shortestVocab + " -> " + shortestTranslation, shortestTranslation + " -> " + shortestVocab, directionAB, directionBA);
}
function directionAB() {
closeBroadcast();
questioningDirection = 'AB';
broadcast("In which mode do you want to practice?", "Text-Mode", "Multiple Choice", chooseTextMode, chooseMultipleChoice)
}
function directionBA() {
closeBroadcast();
questioningDirection = 'BA';
broadcast("In which mode do you want to practice?", "Text-Mode", "Multiple Choice", chooseTextMode, chooseMultipleChoice)
}
function chooseTextMode() {
multipleChoiceMode = false;
showNextCard();
closeBroadcast();
enableTextmodeEnterListener();
}
function chooseMultipleChoice() {
multipleChoiceMode = true;
setupMultipleChoice();
closeBroadcast();
}
function setupMultipleChoice() {
if(flashcards.length < 4) {
console.log("Error while shuffling array. Need more words!");
alert("Error while shuffling your cards! You need at least 4 words for multiple choice");
broadcast("Error while shuffling your cards! You need at least 4 words for multiple choice", "Okay", "", closeBroadcast, closeBroadcast);
document.location.href = '';
} else {
const currentCard = flashcards[currentCardIndex];
const options = createRandomOptions(currentCard);
// Check the questioning direction and set up the correct question text
const questionText = (questioningDirection === 'AB') ? currentCard.vocab : currentCard.translation;
// Display question and options
document.getElementById('question').innerText = questionText;
document.getElementById('answerInput').value = ''; // Clear the answer input
document.getElementById('answerInput').style.display = 'none';
document.getElementById('submitbtn').style.display = 'none';
document.getElementById('nextbtn').style.display = 'none';
// Display multiple choice options
for (let i = 1; i <= 4; i++) {
document.getElementById(`option${i}`).innerText = options[i - 1];
}
document.getElementById('options').style.display = 'block';
updateProgressIndicator();
console.log('[DEBUG] Showing next card in Multiple Choice mode!');
}
}
function createRandomOptions(currentCard) {
// Create an array with the correct answer and three random incorrect answers
const options = [getOtherLanguage(currentCard)];
while (options.length < 4) {
const randomCard = getRandomFlashcard();
const randomOption = getOtherLanguage(randomCard);
if (!options.includes(randomOption)) {
options.push(randomOption);
}
}
// Shuffle the options array
shuffle(options);
return options;
}
function getOtherLanguage(card) {
// Get the vocab or translation of the card depending on the questioning direction
return (questioningDirection === 'AB') ? card.translation : card.vocab;
}
function getRandomFlashcard() {
// Get a random flashcard that is not the current one
let randomCard;
do {
randomCard = flashcards[Math.floor(Math.random() * flashcards.length)];
} while (randomCard === flashcards[currentCardIndex]);
return randomCard;
}
let totalQuestions = 0;
let correctAnswers = 0;
var keysEnabled = true;
// Add event listeners for number keys 1 to 4
document.addEventListener('keydown', function(event) {
if(keysEnabled) {
if (multipleChoiceMode) {
const keyNumber = parseInt(event.key);
if (keyNumber >= 1 && keyNumber <= 4) {
checkAnswerMultipleChoice(keyNumber);
console.log("Keydown 1-4 listener fired: line 520")
}
}
} else {
console.log("Keys are disabled!");
}
});
function makeColorNormalAgain() {
for (let i = 1; i <= 4; i++) {
document.getElementById(`option${i}`).style.backgroundColor = '#3498db';
}
}
// Function to disable all option buttons
function disableOptionButtons() {
for (let i = 1; i <= 4; i++) {
document.getElementById(`option${i}`).classList.add('disabled');
}
}
// Function to enable all option buttons
function enableOptionButtons() {
for (let i = 1; i <= 4; i++) {
document.getElementById(`option${i}`).classList.remove('disabled');
}
}
function disableOptionKeys() {
keysEnabled = false;
}
function enableOptionKeys() {
keysEnabled = true;
}
// Modify the function checkAnswerMultipleChoice to accept the selected option directly
function checkAnswerMultipleChoice(optionNumber) {
const selectedOption = document.getElementById(`option${optionNumber}`).innerText;
const currentCard = flashcards[currentCardIndex];
disableOptionButtons();
disableOptionKeys();
disableTextmodeEnterListener();
// Check the questioning direction and set up the correct answer accordingly
const correctAnswer = (questioningDirection === 'AB') ? currentCard.translation : currentCard.vocab;
if (selectedOption === correctAnswer) {
console.log('[DEBUG] Multiple Choice - Correct Answer!');
//alert(`Correct: ${correctAnswer}!`); //SWITCH TO NEW VISUAL SYSTEM
document.getElementById(`option${optionNumber}`).style.backgroundColor = 'green';
correctAnswers++;
} else {
for (let i = 1; i <= 4; i++) {
const optionText = document.getElementById(`option${i}`).innerText;
if (optionText === correctAnswer) {
document.getElementById(`option${i}`).style.backgroundColor = 'green';
break;
}
}
console.log('[DEBUG] Multiple Choice - Incorrect Answer!');
//alert(`Incorrect. The correct answer is: ${correctAnswer}`);
document.getElementById(`option${optionNumber}`).style.backgroundColor = 'red';
}
totalQuestions++;
currentCardIndex++;
function continueToNext() {
makeColorNormalAgain();
setupMultipleChoice();
document.getElementById('nextbtn').style.display = 'none';
enableOptionKeys();
enableOptionButtons();
}
const keydownListener = function(e) {
if (e.key === 'Enter') {
e.preventDefault();
console.log("Keydown Enter listener fired: line 600")
document.removeEventListener('keydown', keydownListener);
continueToNext();
}
};
const clickListener = function() {
document.removeEventListener('click', clickListener);
continueToNext();
}
if (currentCardIndex < flashcards.length) {
document.getElementById('nextbtn').style.display = 'inline-block';
document.getElementById('nextbtn').addEventListener('click', clickListener);
document.addEventListener('keydown', keydownListener);
console.log('Going to next card!')
//setupMultipleChoice();
} else {
endQuiz();
console.log('[DEBUG] Ended the quiz!');
}
}
function showNextCard() {
if (currentCardIndex < flashcards.length) {
const currentCard = flashcards[currentCardIndex];
// Check the questioning direction and set up the question accordingly
if (questioningDirection === 'AB') {
document.getElementById('question').innerText = currentCard.vocab;
} else {
document.getElementById('question').innerText = currentCard.translation;
}
const correctAnswerOriginalCase = (questioningDirection === 'AB') ? currentCard.translation : currentCard.vocab;
document.getElementById('answerInput').value = '';
console.log('[DEBUG] Showing next card!');
console.log('[DEBUG] currentCardIndex:', currentCardIndex + " / flashcards.length: " + flashcards.length);
updateProgressIndicator();
} else {
endQuiz();
console.log('[DEBUG] Ended the quiz!');
}
}
function updateProgressIndicator() {
const progressBar = document.getElementById('progressBar');
const progressIndicator = document.getElementById('progressIndicator');
const progressValue = (currentCardIndex / flashcards.length) * 100;
progressBar.value = progressValue;
progressIndicator.innerText = `${currentCardIndex} / ${flashcards.length}`;
}
flashcards.forEach(card => {
card.score = 0;
card.attempted = false;
});
function disableSubmitButton() {
document.getElementById('submitbtn').classList.add('disabled');
}
function enableSubmitButton() {
document.getElementById('submitbtn').classList.remove('disabled');
}
function disableAnswerInput() {
document.getElementById('answerInput').disabled = true;
}
function enableAnswerInput() {
document.getElementById('answerInput').disabled = false;
}
function enableTextmodeEnterListener() {
textmodeEnterListenerEnabled = true;
}
function disableTextmodeEnterListener() {
textmodeEnterListenerEnabled = false;
}
let totalScore = 0;
let totalQuestionsText = 0;
let correctAnswersText = 0;
let partiallyCorrectAnswers = 0;
let textmodeEnterListenerEnabled = false;
function checkAnswer() {
enableTextmodeEnterListener();
disableSubmitButton();
disableAnswerInput();
const userAnswer = document.getElementById('answerInput').value.trim().toLowerCase();
const currentCard = flashcards[currentCardIndex];
// Check the questioning direction and set up the correct answer accordingly
const correctAnswer = (questioningDirection === 'AB') ? currentCard.translation.toLowerCase() : currentCard.vocab.toLowerCase();
const correctAnswerOriginalCase = (questioningDirection === 'AB') ? currentCard.translation : currentCard.vocab;
const minLength = Math.min(userAnswer.length, correctAnswer.length);
let correctLettersCount = 0;
for (let i = 0; i < minLength; i++) {
if (userAnswer[i] === correctAnswer[i]) {
correctLettersCount++;
}
}
const accuracy = correctLettersCount / Math.max(userAnswer.length, correctAnswer.length);
const deviation = Math.abs(userAnswer.length - correctAnswer.length);
const symbolsMissing = !userAnswer.includes('/') || !userAnswer.includes(',') || !userAnswer.includes('(') || !userAnswer.includes(')') || !userAnswer.includes('*');
const wordWithMissingSymbols = correctAnswer.replace(/[\/\(\)\*.,]/g, '');
const userAnswerWithMissingSymbols = userAnswer.replace(/[\/\(\)\*.,]/g, '');
const wordWithMissingSymbolsSpace = correctAnswer.replace(/[\/\(\)\*.,]/g, ' ');
const userAnswerWithMissingSymbolsSpace = userAnswer.replace(/[\/\(\)\*.,]/g, ' ');
// Calculate the score based on adjusted accuracy
let score;
if (accuracy >= 1) {
score = 1;
} else if (accuracy >= accuracyThreshold && allowPartialCredit || deviation <= 1 && allowPartialCredit && userAnswer.length >= 4 && accuracy >= 0.9) {
score = 0.5;
} else {
score = 0;
}
// Store the score in the flashcard
flashcards[currentCardIndex].score = score;
flashcards[currentCardIndex].attempted = true;
// Add the score to the total
totalScore += score;
if (accuracy >= 1 || userAnswerWithMissingSymbols.toLowerCase() === wordWithMissingSymbols.toLowerCase() || userAnswerWithMissingSymbolsSpace.toLowerCase() === wordWithMissingSymbolsSpace.toLowerCase()) {
console.log("[DEBUG] Answer is correct!");
console.log("Accuracy: " + accuracy + " | Deviation: " + deviation + " | Symbols Missing: " + symbolsMissing + " | Answer: " + userAnswer + " | Missing Symbols Answer: " + wordWithMissingSymbols + " | UserA wo symbols" + userAnswerWithMissingSymbols + " | Correct Answer: " + correctAnswer);
//alert(`Correct: ${correctAnswerOriginalCase}!`);
document.getElementById(`answerInput`).style.backgroundColor = 'green';
document.getElementById(`answer`).style.visibility = 'visible';
document.getElementById(`answer`).innerHTML = "Correct!";
correctAnswers++;
} else if (score === 0.5) {
console.log('[DEBUG] Answer is partially correct!');
//alert(`Partially Correct: ${correctAnswerOriginalCase}`);
console.log("Accuracy: " + accuracy + " | Deviation: " + deviation + " | Symbols Missing: " + symbolsMissing + " | Answer: " + userAnswer + " | Missing Symbols Answer: " + wordWithMissingSymbols + " | UserA wo symbols" + userAnswerWithMissingSymbols + " | Correct Answer: " + correctAnswer);
document.getElementById(`answerInput`).style.backgroundColor = 'orange';
document.getElementById(`answer`).style.visibility = 'visible';
document.getElementById(`answer`).innerHTML = "Partially correct! The answer is: " + correctAnswerOriginalCase;
partiallyCorrectAnswers++;
} else {
console.log('[DEBUG] Answer is incorrect!');
console.log("Accuracy: " + accuracy + " | Deviation: " + deviation + " | Symbols Missing: " + symbolsMissing + " | Answer: " + userAnswer + " | Missing Symbols Answer: " + wordWithMissingSymbols + " | UserA wo symbols" + userAnswerWithMissingSymbols + " | Correct Answer: " + correctAnswer);
document.getElementById(`answerInput`).style.backgroundColor = 'red';
document.getElementById(`answer`).style.visibility = 'visible';
document.getElementById(`answer`).innerHTML = "Wrong! The answer is: " + correctAnswerOriginalCase;
//alert(`Incorrect. The correct answer is: ${correctAnswerOriginalCase}`);
}
totalQuestions++;
currentCardIndex++;
function continueToNext() {
showNextCard();
document.getElementById('nextbtn').style.display = 'none';
document.getElementById(`answerInput`).style.backgroundColor = 'white';
document.getElementById(`answer`).style.visibility = 'hidden';
enableSubmitButton();
enableAnswerInput();
enableTextmodeEnterListener();
document.addEventListener('keydown', submitEventListener);
console.log('Going to next card!')
}
const clickListener = function() {
document.removeEventListener('click', clickListener);
continueToNext();
}
document.getElementById('nextbtn').style.display = 'inline-block';
document.getElementById('nextbtn').addEventListener('click', clickListener);
const keydownListenerEnter = function(e) {
if (e.key === 'Enter') {
e.preventDefault();
if(textmodeEnterListenerEnabled == "true" || textmodeEnterListenerEnabled == true) { //WHY IN THE FUCKING FUCK IS THIS A FUCKING STRING??!?!?!?! SHOULD BE A GODDAMN BOOLEAN
e.preventDefault();
document.removeEventListener('keydown', keydownListenerEnter);
continueToNext();
console.log("Listener removed. Enter key pressed from textmode because: " + textmodeEnterListenerEnabled);
} else {
document.removeEventListener('keydown', keydownListenerEnter);
console.log("Enter key pressed, but not enabled! Removed Event listener");
}
}
};
document.addEventListener('keydown', keydownListenerEnter);
//TODO: Add enter key functionality for the next button
//showNextCard();
}
const answerInput = document.getElementById('answerInput');
const submitEventListener = function(e) {
if (e.key === 'Enter') {
e.preventDefault();
if(textmodeEnterListenerEnabled == true || textmodeEnterListenerEnabled == "true") {
console.log("Submit Enter listener fired: line 813. EnableTextmodeEnterListener: " + textmodeEnterListenerEnabled)
document.removeEventListener('keydown', submitEventListener);
e.preventDefault();
checkAnswer();
} else {
console.log("Enter key pressed, but not enabled!" + textmodeEnterListenerEnabled);
}
}
}
document.addEventListener('keydown', submitEventListener);
function returnAnswer() {
const currentCard = flashcards[currentCardIndex];
const correctAnswerOriginalCase = (questioningDirection === 'AB') ? currentCard.translation : currentCard.vocab;
console.log(correctAnswerOriginalCase)
}
////////////
//END QUIZ//
////////////
function endQuiz() {
const totalCards = flashcards.length;
const percentageCorrect = (correctAnswers / totalQuestions) * 100;
const totalNormalPoints = correctAnswers;
const totalPartiallyCorrectPoints = partiallyCorrectAnswers / 2;
const totalPoints = totalNormalPoints + totalPartiallyCorrectPoints;
let message = '<b>Well done!</b>';
if (percentageCorrect >= 95) {
message = '<b>Exceptional work, bro!</b>';
} else if (percentageCorrect >= 80) {
message = '<b>Good job, keep it up!</b>';
} else if (percentageCorrect >= 70) {
message = '<b>Not (too) bad!</b>';
} else if (percentageCorrect >= 60) {
message = '<b>You can do better!</b>';
} else if (percentageCorrect >= 50) {
message = '<b>Man, please study!</b>';
} else if (percentageCorrect >= 40) {
message = '<b>You should definetly be studying more!</b>';
} else if (percentageCorrect >= 30) {
message = '<b>Damn, please study!</b>';
} else {
message = "<b>At this point I don't even know what to say</b>"
}
const quizElement = document.getElementById('quiz');
quizElement.innerHTML = `
<h2>Quiz Completed!</h2>
<p>${message}</p>
<p>Correct Answers: ${correctAnswers} (and ${partiallyCorrectAnswers} partially)</p>
<p>Total Questions: ${totalQuestions}</p>
<p>Points: ${totalPoints}</p>
<p>Percentage Correct: ${percentageCorrect.toFixed(2)}%</p>
<div class="diagramCanvas-container">
<canvas class="diagramCanvas" id="diagramCanvas" width="200" height="200"></canvas>
</div>
<button onclick="document.location.href = ''">End</button>
`;
// Get the context of the canvas element
const ctx = document.getElementById('diagramCanvas').getContext('2d');
// Create a pie chart
new Chart(ctx, {
type: 'pie',
data: {
labels: ['Correct', 'Incorrect', 'Partially Correct'],
datasets: [{
data: [correctAnswers, (totalQuestions - partiallyCorrectAnswers) - correctAnswers, partiallyCorrectAnswers],
backgroundColor: ['green', 'red', 'orange'],
}],
},
options: {
responsive: false, // Set to false to fix canvas size
},
});
}
function exportStudySet() {
const formattedStudySet = flashcards.map(card => `${card.vocab}#${card.translation}`).join(';');
const blob = new Blob([formattedStudySet], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'studySet.wvocab';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function resetQuiz() {
currentCardIndex = 0;
startQuiz();
}