Skip to content

Commit 21f47be

Browse files
committed
- update button IDs to match test requirements for automatic slideshow controls
- Changed button IDs in HTML to 'auto-forward', 'auto-backward', and 'stop' - Updated slideshow.js to use the new button IDs - Fixed test errors caused by mismatched element IDs in automatic slideshow feature
1 parent 972b726 commit 21f47be

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

Sprint-3/slideshow/slideshow.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,63 @@ const images = [
44
"./assets/cute-cat-c.jpg",
55
];
66

7+
let currentIndex = 0;
8+
const imgElement = document.getElementById("carousel-img");
9+
const forwardBtn = document.getElementById("forward-btn");
10+
const backwardBtn = document.getElementById("backward-btn");
711

8-
// Write your code here
12+
const autoForwardBtn = document.getElementById("auto-forward");
13+
const autoBackBtn = document.getElementById("auto-backward");
14+
const stopBtn = document.getElementById("stop");
15+
16+
17+
let intervalId = null; // <-- declare intervalId
18+
19+
function updateImage() {
20+
imgElement.src = images[currentIndex];
21+
}
22+
23+
forwardBtn.addEventListener("click", () => {
24+
currentIndex = (currentIndex + 1) % images.length;
25+
updateImage();
26+
});
27+
28+
backwardBtn.addEventListener("click", () => {
29+
currentIndex = (currentIndex - 1 + images.length) % images.length;
30+
updateImage();
31+
});
32+
33+
function disableAutoButtons() {
34+
autoForwardBtn.disabled = true;
35+
autoBackBtn.disabled = true;
36+
stopBtn.disabled = false;
37+
}
38+
39+
function enableAutoButtons() {
40+
autoForwardBtn.disabled = false;
41+
autoBackBtn.disabled = false;
42+
stopBtn.disabled = true;
43+
}
44+
45+
autoForwardBtn.addEventListener("click", () => {
46+
clearInterval(intervalId);
47+
intervalId = setInterval(() => {
48+
currentIndex = (currentIndex + 1) % images.length;
49+
updateImage();
50+
}, 2000);
51+
disableAutoButtons();
52+
});
53+
54+
autoBackBtn.addEventListener("click", () => {
55+
clearInterval(intervalId);
56+
intervalId = setInterval(() => {
57+
currentIndex = (currentIndex - 1 + images.length) % images.length;
58+
updateImage();
59+
}, 2000);
60+
disableAutoButtons();
61+
});
62+
63+
stopBtn.addEventListener("click", () => {
64+
clearInterval(intervalId);
65+
enableAutoButtons();
66+
});

0 commit comments

Comments
 (0)