-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
44 lines (36 loc) · 1.38 KB
/
script.js
File metadata and controls
44 lines (36 loc) · 1.38 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
const flashcards = [
{ question: "What is HTML?", answer: "HyperText Markup Language" },
{ question: "What is CSS?", answer: "Cascading Style Sheets" },
{ question: "What is JavaScript?", answer: "A programming language for web development" }
];
let currentCardIndex = 0;
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');
function updateCard() {
cardFront.textContent = flashcards[currentCardIndex].question;
cardBack.textContent = flashcards[currentCardIndex].answer;
}
function getRandomIndex() {
let newIndex;
do {
newIndex = Math.floor(Math.random() * flashcards.length);
} while (newIndex === currentCardIndex && flashcards.length > 1);
return newIndex;
}
prevBtn.addEventListener('click', () => {
currentCardIndex = (currentCardIndex - 1 + flashcards.length) % flashcards.length;
updateCard();
});
nextBtn.addEventListener('click', () => {
currentCardIndex = (currentCardIndex + 1) % flashcards.length;
updateCard();
});
randomBtn.addEventListener('click', () => {
currentCardIndex = getRandomIndex();
updateCard();
});
// Initialize the first card
updateCard();