Skip to content

Commit 29f86d8

Browse files
author
Jason Tigas
committed
Allow retry for questions under 50% accuracy and improve UI
1 parent 5ce6276 commit 29f86d8

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

index.html

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
border-radius: 10px;
2020
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
2121
text-align: center;
22+
position: relative;
2223
}
2324
.question {
2425
font-size: 1.5em;
@@ -38,38 +39,87 @@
3839
<div class="question" id="question">Loading...</div>
3940
<button id="rightButton" style="background-color: #4CAF50; color: white; margin-right: 1em;">Right</button>
4041
<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>
4247
</div>
4348

4449
<script>
50+
const savedStats = localStorage.getItem('flashcardStats');
51+
const stats = savedStats ? JSON.parse(savedStats) : {};
52+
let currentQuestion = null;
53+
4554
fetch('questions.json')
4655
.then(response => response.json())
4756
.then(data => {
4857
const questions = data.questions;
58+
questions.forEach(q => {
59+
if (!stats[q]) {
60+
stats[q] = { right: 0, shown: 0 };
61+
}
62+
});
4963
let score = 0;
5064

5165
function nextQuestion() {
66+
console.log(questions);
5267
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+
5587
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;
5694
} else {
5795
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);
58101
}
59102
}
60103

61104
// Make it globally accessible to the button
62105
window.nextQuestion = nextQuestion;
63106

64107
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+
}
67114
nextQuestion();
68115
});
69116

70117
document.getElementById('wrongButton').addEventListener('click', () => {
71118
nextQuestion();
72119
});
120+
document.getElementById('restartButton').addEventListener('click', () => {
121+
location.reload();
122+
});
73123

74124
// Load the first question
75125
nextQuestion();

0 commit comments

Comments
 (0)