-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.js
More file actions
85 lines (71 loc) · 2.9 KB
/
Copy pathcards.js
File metadata and controls
85 lines (71 loc) · 2.9 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
function initializeCards(flashcards) {
let currentCardIndex = 0;
const flashcard = document.querySelector('.flashcard');
const cardFront = document.querySelector('.card-front p');
const cardBack = document.querySelector('.card-back p');
const prevBtn = document.getElementById('prevBtn');
const nextBtn = document.getElementById('nextBtn');
const randomBtn = document.getElementById('randomBtn');
const stillLearningBtn = document.getElementById('stillLearningBtn');
const knewThisBtn = document.getElementById('knewThisBtn');
function getActiveCards() {
return flashcards.filter(card => card.active);
}
function getNextActiveIndex(direction) {
let newIndex = currentCardIndex;
do {
newIndex = (newIndex + direction + flashcards.length) % flashcards.length;
} while (!flashcards[newIndex].active && getActiveCards().length > 1);
return newIndex;
}
function getRandomIndex() {
const activeCards = getActiveCards();
if (activeCards.length <= 1) return currentCardIndex;
let newIndex;
do {
newIndex = Math.floor(Math.random() * flashcards.length);
} while (!flashcards[newIndex].active || newIndex === currentCardIndex);
return newIndex;
}
function updateCard() {
cardFront.textContent = flashcards[currentCardIndex].question;
cardBack.textContent = flashcards[currentCardIndex].answer;
const cardInner = flashcard.querySelector('.card-inner');
cardInner.classList.remove('is-flipped');
}
flashcard.addEventListener('click', (event) => {
if (!event.target.closest('button')) {
const cardInner = flashcard.querySelector('.card-inner');
cardInner.classList.toggle('is-flipped');
}
});
prevBtn.addEventListener('click', () => {
currentCardIndex = getNextActiveIndex(-1);
updateCard();
});
nextBtn.addEventListener('click', () => {
currentCardIndex = getNextActiveIndex(1);
updateCard();
});
randomBtn.addEventListener('click', () => {
currentCardIndex = getRandomIndex();
updateCard();
});
stillLearningBtn.addEventListener('click', (event) => {
event.stopPropagation();
currentCardIndex = getNextActiveIndex(1);
updateCard();
});
knewThisBtn.addEventListener('click', (event) => {
event.stopPropagation();
flashcards[currentCardIndex].active = false;
if (getActiveCards().length === 0) {
flashcards.forEach(card => card.active = true);
alert("Great job! All cards completed. Starting over!");
}
currentCardIndex = getNextActiveIndex(1);
updateCard();
});
// Initialize the first card
updateCard();
}