-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (55 loc) · 1.69 KB
/
script.js
File metadata and controls
65 lines (55 loc) · 1.69 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
let cards = JSON.parse(localStorage.getItem('cards')) || [];
let currentIndex = 0;
let showingFront = true;
const cardContainer = document.getElementById('card-container');
const flipBtn = document.getElementById('flip-btn');
const rememberedBtn = document.getElementById('remembered-btn');
const forgotBtn = document.getElementById('forgot-btn');
const addBtn = document.getElementById('add-card-btn');
const frontInput = document.getElementById('front-input');
const backInput = document.getElementById('back-input');
function showCard() {
if (!cards.length) {
cardContainer.textContent = "No cards yet.";
return;
}
const card = cards[currentIndex];
cardContainer.textContent = showingFront ? card.front : card.back;
}
function flipCard() {
showingFront = !showingFront;
showCard();
}
function addCard() {
const front = frontInput.value.trim();
const back = backInput.value.trim();
if (front && back) {
cards.push({ front, back, score: 0 });
localStorage.setItem('cards', JSON.stringify(cards));
frontInput.value = '';
backInput.value = '';
currentIndex = cards.length - 1;
showingFront = true;
showCard();
}
}
function remembered() {
cards[currentIndex].score++;
nextCard();
}
function forgot() {
cards[currentIndex].score = Math.max(cards[currentIndex].score - 1, 0);
nextCard();
}
function nextCard() {
cards.sort((a, b) => a.score - b.score);
currentIndex = 0;
showingFront = true;
showCard();
localStorage.setItem('cards', JSON.stringify(cards));
}
flipBtn.onclick = flipCard;
addBtn.onclick = addCard;
rememberedBtn.onclick = remembered;
forgotBtn.onclick = forgot;
showCard();