-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
932 lines (793 loc) · 25.2 KB
/
index.html
File metadata and controls
932 lines (793 loc) · 25.2 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>True/False Practice</title>
<!-- MathJax for LaTeX rendering -->
<script>
window.MathJax = {
tex: { inlineMath: [['$', '$'], ['\\(', '\\)']] },
options: { skipHtmlTags: ['script','noscript','style','textarea','pre','code'] }
};
</script>
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<style>
:root{
/* ===== Harvard Crimson Tech Theme ===== */
--bg0: #b23a4b; /* lighter top wash */
--bg1: #631421; /* your chosen crimson */
--bg0: #2a0b12; /* deep crimson plum */
--bg1: #070409; /* near-black with warmth */
--text: rgba(255,255,255,0.95);
--muted: rgba(255,255,255,0.70);
--primaryA: white;
--primaryB: white;
--primaryHoverA: #631421;
--primaryHoverB: #A8323F;
--ring: rgba(199,62,77,0.35);
--radius: 16px;
--shadow: 0 12px 30px rgba(0,0,0,0.45);
--shadow2: 0 8px 18px rgba(0,0,0,0.30);
}
* { box-sizing: border-box; }
body { min-height: 100vh; }
body{
margin: 0;
padding: 28px 18px 60px;
font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
color: var(--text);
background:
/* soft corner light */
radial-gradient(900px 600px at 80% 85%, rgba(255,255,255,0.18), transparent 60%),
/* brushed metal streaks */
repeating-linear-gradient(
90deg,
rgba(255,255,255,0.05) 0px,
rgba(0,0,0,0.06) 2px,
rgba(255,255,255,0.03) 4px,
rgba(0,0,0,0.05) 6px
),
/* subtle tech grid */
linear-gradient(rgba(255,255,255,0.06) 1px, transparent 1px),
linear-gradient(90deg, rgba(255,255,255,0.06) 1px, transparent 1px),
/* metallic base gradient */
linear-gradient(
180deg,
#0f0f12 0%,
#2b2c31 28%,
#1a1b1f 55%,
#3a3b40 78%,
#121216 100%
);
background-size:
auto,
auto,
56px 56px,
56px 56px,
auto;
background-blend-mode: screen, overlay, normal, normal, normal;
}
.container{
max-width: 980px;
margin: 0 auto;
position: relative;
z-index: 1;
}
h1{
margin: 0 0 8px;
font-size: 28px;
letter-spacing: -0.02em;
}
.muted{
color: var(--muted);
margin-top: 0;
line-height: 1.5;
}
/* Cards */
.card{
border: 1px solid rgba(0,0,0,0.08);
background: #ffffff;
color: #111111;
border-radius: var(--radius);
padding: 18px;
margin: 16px 0;
box-shadow: 0 10px 30px rgba(0,0,0,0.15);
}
.card .muted{ color: #444; }
.card strong{ color: #000; }
.qhead{
display: flex;
justify-content: space-between;
gap: 12px;
align-items: center;
margin-bottom: 8px;
}
.qnum{ font-weight: 650; }
.choices{
margin-top: 12px;
display: flex;
gap: 18px;
flex-wrap: wrap;
}
input[type="radio"]{
accent-color: var(--primaryHoverB);
transform: translateY(1px);
}
/* Buttons */
button{
padding: 11px 14px;
border-radius: 12px;
border: 1.5px solid #000;
background: rgba(255,255,255,0.06);
color: var(--text);
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}
button:hover{
background: rgba(255,255,255,0.10);
transform: translateY(-1px);
}
button:focus{
outline: none;
box-shadow: 0 0 0 4px var(--ring);
}
button:disabled{
opacity: 0.55;
cursor: not-allowed;
transform: none;
}
button.primary{
background: linear-gradient(135deg, var(--primaryA), var(--primaryB));
color: rgba(10,14,22,0.95);
font-weight: 650;
}
button.primary:hover{
background: linear-gradient(135deg, var(--primaryHoverA), var(--primaryHoverB));
color: rgba(255,255,255,0.95);
}
button.secondary{
background: rgba(255,255,255,0.08);
color: rgba(10,14,22,0.95);
}
button.secondary:hover{
background: linear-gradient(135deg, var(--primaryHoverA), var(--primaryHoverB));
color: rgba(255,255,255,0.95);
}
.start-wrapper{
display: flex;
justify-content: center;
margin-top: 20px;
}
.start-button{
padding: 16px 32px;
font-size: 18px;
border-radius: 14px;
min-width: 220px;
letter-spacing: 0.3px;
}
/* Result blocks */
.result{
margin-top: 12px;
padding: 12px;
border-radius: 12px;
border: 1px solid rgba(0,0,0,0.08);
background: #f8f9fa;
color: #111;
}
.result.correct{
border-color: #34d399;
background: #ecfdf5;
}
.result.incorrect{
border-color: #fb7185;
background: #fef2f2;
}
.explain{
margin-top: 8px;
color: #333;
}
.score{
font-size: 18px;
font-weight: 750;
margin: 18px 0 8px;
}
.footerbar{
margin: 22px 0 60px;
display: flex;
justify-content: flex-end;
position: sticky;
bottom: 12px;
}
.footerbar > button{
box-shadow: var(--shadow2);
}
.hidden{ display: none; }
.notice{
border: 1px solid rgba(0,0,0,0.10);
border-radius: var(--radius);
padding: 14px;
background: rgba(255,255,255,0.85);
margin: 16px 0;
color: #111;
}
.mono{
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
/* Tag Boxes */
.tagBox{
margin: 14px 0 6px;
padding: 12px;
border-radius: 14px;
border: 1px solid rgba(0,0,0,0.10);
background: rgba(255,255,255,0.85);
color: #111;
}
.tagTitle{
font-weight: 650;
margin-bottom: 10px;
}
.tagOptions{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
gap: 8px 14px;
}
.tagOptions label{
display: flex;
gap: 10px;
align-items: center;
padding: 6px 8px;
border-radius: 10px;
}
.tagOptions input[type="checkbox"]{
transform: translateY(1px);
accent-color: var(--primaryHoverB); /* was var(--bg1) */
}
.tagActions{
display: flex;
gap: 10px;
margin-top: 10px;
flex-wrap: wrap;
}
/* Attempt log UI */
.logActions{
display: flex;
gap: 10px;
margin-top: 10px;
flex-wrap: wrap;
}
.logTable{
width: 100%;
border-collapse: collapse;
margin-top: 10px;
font-size: 14px;
}
.logTable th, .logTable td{
text-align: left;
padding: 8px 10px;
border-bottom: 1px solid rgba(0,0,0,0.08);
vertical-align: top;
}
.logTable th{
font-weight: 700;
color: #111;
}
.logTable td{
color: #111;
}
.pill{
display: inline-block;
padding: 2px 8px;
border-radius: 999px;
border: 1px solid rgba(0,0,0,0.12);
background: rgba(255,255,255,0.8);
margin: 2px 6px 2px 0;
font-size: 12px;
}
.smallMuted{
color: #555;
font-size: 13px;
margin-top: 6px;
}
</style>
<!-- Config and Question bank (separate files) -->
<script src="./config.js"></script>
<script src="./questions.js"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
const CONFIG = window.APP_CONFIG;
document.getElementById("app-title").textContent = CONFIG.title;
document.getElementById("app-intro").innerHTML = CONFIG.intro;
});
</script>
</head>
<body>
<div class="container">
<h1 id="app-title"></h1>
<!-- Start screen -->
<div id="startScreen" class="card">
<p class="muted" id="app-intro"></p>
<div class="tagBox">
<div class="tagTitle">Choose topics:</div>
<div id="tagOptions" class="tagOptions"></div>
<div class="tagActions">
<button id="selectAllBtn" class="secondary" type="button">Select all</button>
<button id="selectNoneBtn" class="secondary" type="button">Deselect all </button>
</div>
</div>
<div id="rateLimitBox" class="notice hidden"></div>
<div class="start-wrapper">
<button id="startBtn" class="primary start-button">Start Practice</button>
</div>
<div class="logActions">
<button id="toggleLogBtn" class="secondary" type="button">Show attempt log</button>
<button id="clearLogBtn" class="secondary" type="button">Clear log</button>
</div>
<div id="attemptLogBox" class="notice hidden">
<div><strong>Attempt log (this device/browser)</strong></div>
<div id="attemptLogBody"></div>
<div class="smallMuted">
Stored locally in your browser (localStorage). Clearing site data or using a different browser/device will change what you see.
</div>
</div>
<p class="muted">
Note: Attempts are limited to <strong>one every 10 minutes</strong> on this device/browser.
</p>
</div>
<!-- Quiz screen -->
<div id="quizScreen" class="hidden">
<p class="muted">Answer the following 10 questions, then click submit at the bottom.</p>
<div id="quiz"></div>
<div id="summary"></div>
<div class="footerbar">
<button id="submitBtn" class="primary">Submit</button>
</div>
</div>
<script>
// ----------------------------
// Config
// ----------------------------
const ATTEMPT_COOLDOWN_MS = 10 * 60 * 1000;
const ATTEMPT_KEY = "la_tf_last_attempt_started_at_ms";
const MIN_QUESTIONS_PER_ATTEMPT = 10;
// Attempt logging
const ATTEMPT_LOG_KEY = "la_tf_attempt_log_v1";
const MAX_LOG_ENTRIES = 200;
// ----------------------------
// Load bank (from questions.js)
// ----------------------------
const QUESTION_BANK = (window.QUESTION_BANK ?? []);
if (!Array.isArray(QUESTION_BANK) || QUESTION_BANK.length === 0) {
console.warn("QUESTION_BANK is empty or missing. Check questions.js");
}
// ----------------------------
// Tags (topic selection)
// ----------------------------
const CONFIG = window.APP_CONFIG || {};
const ALL_TAGS = CONFIG.categories || [];
const TAGS_KEY = "la_tf_selected_tags_v1";
function loadSelectedTags() {
try {
const raw = localStorage.getItem(TAGS_KEY);
if (!raw) return new Set(ALL_TAGS); // default: all selected
const arr = JSON.parse(raw);
const s = new Set((arr || []).filter(t => ALL_TAGS.includes(t)));
return s.size ? s : new Set(); // allow empty (means no topics)
} catch {
return new Set(ALL_TAGS);
}
}
function saveSelectedTags(tagSet) {
localStorage.setItem(TAGS_KEY, JSON.stringify([...tagSet]));
}
function getFilteredQuestionPool() {
const selected = loadSelectedTags();
if (selected.size === 0) return [];
return QUESTION_BANK.filter(q => {
const tags = Array.isArray(q.tags) ? q.tags : [];
return tags.some(t => selected.has(t));
});
}
function updateStartAvailability() {
const pool = getFilteredQuestionPool();
const enough = pool.length >= MIN_QUESTIONS_PER_ATTEMPT;
if (!enough) {
rateLimitBox.classList.remove("hidden");
const sel = loadSelectedTags();
const msg = (sel.size === 0)
? "Please select at least one topic."
: `Please select more topics.`;
rateLimitBox.innerHTML = `
<div><strong>Not enough questions for the selected topics.</strong></div>
<div>${msg} Current pool size: <strong>${pool.length}</strong> (need ${MIN_QUESTIONS_PER_ATTEMPT}).</div>
`;
startBtn.disabled = true;
} else {
if (msRemaining() <= 0) rateLimitBox.classList.add("hidden");
startBtn.disabled = false;
}
}
function renderTagOptions() {
const tagOptionsEl = document.getElementById("tagOptions");
const selectAllBtn = document.getElementById("selectAllBtn");
const selectNoneBtn = document.getElementById("selectNoneBtn");
if (!tagOptionsEl || !selectAllBtn || !selectNoneBtn) return;
function paint() {
const selected = loadSelectedTags();
const counts = Object.fromEntries(ALL_TAGS.map(t => [t, 0]));
for (const q of QUESTION_BANK) {
const tags = Array.isArray(q.tags) ? q.tags : [];
for (const t of tags) if (counts[t] !== undefined) counts[t]++;
}
tagOptionsEl.innerHTML = ALL_TAGS.map(tag => {
const checked = selected.has(tag) ? "checked" : "";
const count = counts[tag] ?? 0;
return `
<label>
<input type="checkbox" value="${tag}" ${checked}/>
<span>${tag} <span style="color:#666">(${count})</span></span>
</label>
`;
}).join("");
}
function readSelectionFromDOM() {
const s = new Set();
tagOptionsEl.querySelectorAll("input[type='checkbox']").forEach(cb => {
if (cb.checked) s.add(cb.value);
});
return s;
}
paint();
tagOptionsEl.addEventListener("change", () => {
saveSelectedTags(readSelectionFromDOM());
showRateLimitNotice();
});
selectAllBtn.addEventListener("click", () => {
saveSelectedTags(new Set(ALL_TAGS));
paint();
showRateLimitNotice();
});
selectNoneBtn.addEventListener("click", () => {
saveSelectedTags(new Set());
paint();
showRateLimitNotice();
});
showRateLimitNotice();
}
// ----------------------------
// Attempt log helpers (localStorage)
// ----------------------------
function loadAttemptLog() {
try {
const raw = localStorage.getItem(ATTEMPT_LOG_KEY);
const arr = raw ? JSON.parse(raw) : [];
return Array.isArray(arr) ? arr : [];
} catch {
return [];
}
}
function saveAttemptLog(entries) {
const trimmed = entries.slice(-MAX_LOG_ENTRIES);
localStorage.setItem(ATTEMPT_LOG_KEY, JSON.stringify(trimmed));
}
function safeUUID() {
return (crypto?.randomUUID
? crypto.randomUUID()
: String(Date.now()) + "-" + Math.random().toString(16).slice(2)
);
}
function logAttemptStart({ selectedTags, questionCount, questionIds }) {
const log = loadAttemptLog();
const entry = {
id: safeUUID(),
startedAtMs: Date.now(),
selectedTags: [...selectedTags],
questionCount,
questionIds: questionIds ?? null,
submittedAtMs: null,
score: null,
total: null
};
log.push(entry);
saveAttemptLog(log);
return entry.id;
}
function logAttemptSubmit({ attemptId, score, total }) {
const log = loadAttemptLog();
const i = log.findIndex(e => e.id === attemptId);
if (i >= 0) {
log[i].submittedAtMs = Date.now();
log[i].score = score;
log[i].total = total;
saveAttemptLog(log);
}
}
function clearAttemptLog() {
localStorage.removeItem(ATTEMPT_LOG_KEY);
}
// ----------------------------
// Helpers
// ----------------------------
function shuffle(arr) {
const a = arr.slice();
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function pickRandomQuestions(k) {
const pool = getFilteredQuestionPool();
const shuffled = shuffle(pool);
return shuffled.slice(0, Math.min(k, shuffled.length));
}
function msRemaining() {
const last = Number(localStorage.getItem(ATTEMPT_KEY));
if (!Number.isFinite(last) || last <= 0) return 0;
const elapsed = Date.now() - last;
return Math.max(0, ATTEMPT_COOLDOWN_MS - elapsed);
}
function fmtTime(ms) {
const totalSec = Math.ceil(ms / 1000);
const min = Math.floor(totalSec / 60);
const sec = totalSec % 60;
return `${min}:${String(sec).padStart(2, "0")}`;
}
function fmtDateTime(ms) {
try {
return new Date(ms).toLocaleString();
} catch {
return String(ms);
}
}
function typesetMath() {
if (window.MathJax?.typesetPromise) {
window.MathJax.typesetPromise();
}
}
// ----------------------------
// State
// ----------------------------
let current = [];
let responses = {}; // idx -> true/false
let currentAttemptId = null;
// ----------------------------
// Elements
// ----------------------------
const startScreen = document.getElementById("startScreen");
const quizScreen = document.getElementById("quizScreen");
const startBtn = document.getElementById("startBtn");
const submitBtn = document.getElementById("submitBtn");
const quizEl = document.getElementById("quiz");
const summaryEl = document.getElementById("summary");
const rateLimitBox = document.getElementById("rateLimitBox");
const toggleLogBtn = document.getElementById("toggleLogBtn");
const clearLogBtn = document.getElementById("clearLogBtn");
const attemptLogBox = document.getElementById("attemptLogBox");
const attemptLogBody = document.getElementById("attemptLogBody");
let countdownTimer = null;
function showRateLimitNotice() {
const rem = msRemaining();
if (rem > 0) {
rateLimitBox.classList.remove("hidden");
rateLimitBox.innerHTML = `
<div><strong>Attempt limit reached.</strong></div>
<div>Please wait <span class="mono">${fmtTime(rem)}</span> before starting a new attempt.</div>
`;
startBtn.disabled = true;
if (countdownTimer) clearInterval(countdownTimer);
countdownTimer = setInterval(() => {
const r = msRemaining();
if (r <= 0) {
clearInterval(countdownTimer);
countdownTimer = null;
showRateLimitNotice();
} else {
const mono = rateLimitBox.querySelector(".mono");
if (mono) mono.textContent = fmtTime(r);
}
}, 250);
return;
}
if (countdownTimer) {
clearInterval(countdownTimer);
countdownTimer = null;
}
updateStartAvailability();
}
// ----------------------------
// Attempt log UI (toggle + auto-refresh)
// ----------------------------
function renderAttemptLog() {
const log = loadAttemptLog();
const entries = log.slice().reverse(); // newest first
if (entries.length === 0) {
attemptLogBody.innerHTML = `<div>No attempts logged yet on this device/browser.</div>`;
return;
}
attemptLogBody.innerHTML = `
<table class="logTable">
<thead>
<tr>
<th>Started</th>
<th>Submitted</th>
<th>Score</th>
<th>Topics</th>
</tr>
</thead>
<tbody>
${entries.map(e => {
const started = fmtDateTime(e.startedAtMs);
const submitted = e.submittedAtMs ? fmtDateTime(e.submittedAtMs) : "—";
const score = (Number.isFinite(e.score) && Number.isFinite(e.total)) ? `${e.score}/${e.total}` : "—";
const topics = Array.isArray(e.selectedTags) && e.selectedTags.length
? e.selectedTags.map(t => `<span class="pill">${t}</span>`).join("")
: `<span class="pill">none</span>`;
return `
<tr>
<td>${started}</td>
<td>${submitted}</td>
<td>${score}</td>
<td>${topics}</td>
</tr>
`;
}).join("")}
</tbody>
</table>
`;
}
function setLogVisible(visible) {
if (visible) {
renderAttemptLog();
attemptLogBox.classList.remove("hidden");
toggleLogBtn.textContent = "Hide attempt log";
} else {
attemptLogBox.classList.add("hidden");
toggleLogBtn.textContent = "Show attempt log";
}
}
function isLogVisible() {
return !attemptLogBox.classList.contains("hidden");
}
function refreshLogIfVisible() {
if (isLogVisible()) renderAttemptLog();
}
// ----------------------------
// Rendering quiz
// ----------------------------
function renderQuiz() {
summaryEl.innerHTML = "";
quizEl.innerHTML = "";
current.forEach((q, idx) => {
const card = document.createElement("div");
card.className = "card";
card.innerHTML = `
<div class="qhead">
<div class="qnum">Q${idx + 1}</div>
</div>
<div class="statement">${q.statement}</div>
<div class="choices">
<label>
<input type="radio" name="q${idx}" value="true" ${responses[idx] === true ? "checked" : ""}/>
True
</label>
<label>
<input type="radio" name="q${idx}" value="false" ${responses[idx] === false ? "checked" : ""}/>
False
</label>
</div>
<div id="feedback-${idx}"></div>
`;
card.querySelectorAll(`input[name="q${idx}"]`).forEach(input => {
input.addEventListener("change", (e) => {
responses[idx] = (e.target.value === "true");
});
});
quizEl.appendChild(card);
});
typesetMath();
}
function gradeQuiz() {
let correctCount = 0;
current.forEach((q, idx) => {
const picked = responses[idx];
const isAnswered = (picked === true || picked === false);
const isCorrect = isAnswered && (picked === q.answer);
if (isCorrect) correctCount++;
const feedback = document.getElementById(`feedback-${idx}`);
const correctText = q.answer ? "True" : "False";
const yourText = isAnswered ? (picked ? "True" : "False") : "No answer";
feedback.innerHTML = `
<div class="result ${isCorrect ? "correct" : "incorrect"}">
<div><strong>${isCorrect ? "Correct" : "Incorrect"}</strong></div>
<div>Your answer: ${yourText}</div>
<div>Correct answer: ${correctText}</div>
<div class="explain"><em>Explanation:</em> ${q.explanation ?? "—"}</div>
</div>
`;
});
summaryEl.innerHTML = `<div class="score">Score: ${correctCount} / ${current.length}</div>`;
typesetMath();
window.scrollTo({ top: 0, behavior: "smooth" });
if (currentAttemptId) {
logAttemptSubmit({
attemptId: currentAttemptId,
score: correctCount,
total: current.length
});
// Auto-refresh the log after each completed attempt (if visible)
refreshLogIfVisible();
}
}
function startAttempt() {
const rem = msRemaining();
if (rem > 0) {
showRateLimitNotice();
return;
}
const pool = getFilteredQuestionPool();
if (pool.length < MIN_QUESTIONS_PER_ATTEMPT) {
updateStartAvailability();
return;
}
localStorage.setItem(ATTEMPT_KEY, String(Date.now()));
current = pickRandomQuestions(MIN_QUESTIONS_PER_ATTEMPT);
responses = {};
const selected = loadSelectedTags();
const questionIds = current.map(q => (q.id ?? null)); // only meaningful if you have q.id in questions.js
currentAttemptId = logAttemptStart({
selectedTags: selected,
questionCount: current.length,
questionIds
});
// If the log is visible, refresh so the new "started" row shows immediately
refreshLogIfVisible();
startScreen.classList.add("hidden");
quizScreen.classList.remove("hidden");
hasSubmitted = false;
submitBtn.textContent = "Submit";
renderQuiz();
}
// ----------------------------
// Events
// ----------------------------
startBtn.addEventListener("click", startAttempt);
toggleLogBtn.addEventListener("click", () => {
setLogVisible(!isLogVisible());
});
clearLogBtn.addEventListener("click", () => {
const ok = confirm("Clear the attempt log on this device/browser?");
if (!ok) return;
clearAttemptLog();
refreshLogIfVisible();
if (!isLogVisible()) {
// If hidden, keep it hidden; button state unchanged.
// If visible, it will now show empty state via refresh.
}
});
let hasSubmitted = false;
function handleSubmitButton() {
if (!hasSubmitted) {
gradeQuiz();
hasSubmitted = true;
submitBtn.textContent = "Back to Start";
} else {
quizScreen.classList.add("hidden");
startScreen.classList.remove("hidden");
submitBtn.textContent = "Submit";
hasSubmitted = false;
showRateLimitNotice();
// Also refresh log if visible (useful if user cleared data in another tab, etc.)
refreshLogIfVisible();
window.scrollTo({ top: 0, behavior: "smooth" });
}
}
submitBtn.addEventListener("click", handleSubmitButton);
// On load
renderTagOptions();
showRateLimitNotice();
setLogVisible(false); // initialize toggle text consistently
</script>
</div>
</body>
</html>