|
19 | 19 | border-radius: 10px; |
20 | 20 | box-shadow: 0 4px 8px rgba(0,0,0,0.1); |
21 | 21 | text-align: center; |
| 22 | + position: relative; |
22 | 23 | } |
23 | 24 | .question { |
24 | 25 | font-size: 1.5em; |
|
38 | 39 | <div class="question" id="question">Loading...</div> |
39 | 40 | <button id="rightButton" style="background-color: #4CAF50; color: white; margin-right: 1em;">Right</button> |
40 | 41 | <button id="wrongButton" style="background-color: #f44336; color: white;">Wrong</button> |
41 | | - <div id="score" style="font-size: 1.2em; margin-top: 1em;">Score: 0</div> |
| 42 | + <button id="restartButton" style="display: none; background-color: #2196F3; color: white; margin-top: 1em;">Restart</button> |
| 43 | + <div style="margin-top: 1em; text-align: center;"> |
| 44 | + <div id="score" style="font-size: 1.2em;">Score: 0</div> |
| 45 | + <div id="cardStats" style="font-size: 0.8em; color: #555;"></div> |
| 46 | + </div> |
42 | 47 | </div> |
43 | 48 |
|
44 | 49 | <script> |
| 50 | + const savedStats = localStorage.getItem('flashcardStats'); |
| 51 | + const stats = savedStats ? JSON.parse(savedStats) : {}; |
| 52 | + let currentQuestion = null; |
| 53 | + |
45 | 54 | fetch('questions.json') |
46 | 55 | .then(response => response.json()) |
47 | 56 | .then(data => { |
48 | 57 | const questions = data.questions; |
| 58 | + questions.forEach(q => { |
| 59 | + if (!stats[q]) { |
| 60 | + stats[q] = { right: 0, shown: 0 }; |
| 61 | + } |
| 62 | + }); |
49 | 63 | let score = 0; |
50 | 64 |
|
51 | 65 | function nextQuestion() { |
| 66 | + console.log(questions); |
52 | 67 | if (questions.length > 0) { |
53 | | - const randomIndex = Math.floor(Math.random() * questions.length); |
54 | | - const question = questions.splice(randomIndex, 1)[0]; // remove the selected question |
| 68 | + // Allow questions with <50% right to appear again |
| 69 | + const eligible = [...questions]; |
| 70 | + Object.entries(stats).forEach(([q, s]) => { |
| 71 | + const pct = s.shown > 0 ? s.right / s.shown : 1; |
| 72 | + if (pct < 0.5 && !questions.includes(q)) { |
| 73 | + eligible.push(q); // retry if under 50% and already shown |
| 74 | + console.log("Eligible question: ", q); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + const randomIndex = Math.floor(Math.random() * eligible.length); |
| 79 | + const question = eligible[randomIndex]; |
| 80 | + |
| 81 | + // Remove from the original questions array if it's still there |
| 82 | + const indexInQuestions = questions.indexOf(question); |
| 83 | + if (indexInQuestions !== -1) { |
| 84 | + questions.splice(indexInQuestions, 1); |
| 85 | + } |
| 86 | + |
55 | 87 | document.getElementById('question').innerText = question; |
| 88 | + const s = stats[question]; |
| 89 | + const pct = s.shown > 0 ? Math.round((s.right / s.shown) * 100) : 0; |
| 90 | + document.getElementById('cardStats').innerText = `${pct}%`; |
| 91 | + stats[question].shown++; |
| 92 | + localStorage.setItem('flashcardStats', JSON.stringify(stats)); |
| 93 | + currentQuestion = question; |
56 | 94 | } else { |
57 | 95 | document.getElementById('question').innerText = 'Great job! You finished!'; |
| 96 | + document.getElementById('cardStats').innerText = ''; |
| 97 | + document.getElementById('rightButton').style.display = 'none'; |
| 98 | + document.getElementById('wrongButton').style.display = 'none'; |
| 99 | + document.getElementById('restartButton').style.display = 'inline-block'; |
| 100 | + console.table(stats); |
58 | 101 | } |
59 | 102 | } |
60 | 103 |
|
61 | 104 | // Make it globally accessible to the button |
62 | 105 | window.nextQuestion = nextQuestion; |
63 | 106 |
|
64 | 107 | document.getElementById('rightButton').addEventListener('click', () => { |
65 | | - score++; |
66 | | - document.getElementById('score').innerText = `Score: ${score}`; |
| 108 | + if (currentQuestion) { |
| 109 | + stats[currentQuestion].right++; |
| 110 | + localStorage.setItem('flashcardStats', JSON.stringify(stats)); |
| 111 | + score++; |
| 112 | + document.getElementById('score').innerText = `Score: ${score}`; |
| 113 | + } |
67 | 114 | nextQuestion(); |
68 | 115 | }); |
69 | 116 |
|
70 | 117 | document.getElementById('wrongButton').addEventListener('click', () => { |
71 | 118 | nextQuestion(); |
72 | 119 | }); |
| 120 | + document.getElementById('restartButton').addEventListener('click', () => { |
| 121 | + location.reload(); |
| 122 | + }); |
73 | 123 |
|
74 | 124 | // Load the first question |
75 | 125 | nextQuestion(); |
|
0 commit comments