Skip to content

Commit 6486f91

Browse files
committed
Chaleng level one manuel forward and backward buttons functions
1 parent b6e7bd0 commit 6486f91

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

Sprint-3/slideshow/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6-
<title>Title here</title>
6+
<title>Image carousel</title>
77
<script defer src="slideshow.js"></script>
88
</head>
99
<body>

Sprint-3/slideshow/slideshow.js

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,80 @@
1+
2+
// Write your code here
3+
// ARRAY OF IMAGES: This stores the file paths or URLs of all images in our carousel
4+
// We need at least 4 images as specified in the requirements
15
const images = [
2-
"./assets/cute-cat-a.png",
3-
"./assets/cute-cat-b.jpg",
4-
"./assets/cute-cat-c.jpg",
6+
"./assets/cute-cat-a.png", // Local file in assets folder
7+
"./assets/cute-cat-c.jpg",
8+
"https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Cat_August_2010-4.jpg/1280px-Cat_August_2010-4.jpg",
9+
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQbw_nRUZfGp9Bma2RQDW_5l5apC36i7Rfp16ahrhvsXrUp6pmlShZH546EisOrwCsrPP9Bdlob5pFltdHlzjGOgcsobmsPP6Rl950b9iLmAw&s=10", // Local file in assets folder
510
];
611

12+
// CURRENT IMAGE INDEX: This variable keeps track of which image we're currently showing
13+
// We start at 0 because array indexes start at 0 (0 = first image, 1 = second image, etc.)
14+
let currentImageIndex = 0;
15+
16+
// GET HTML ELEMENTS: We find the HTML elements we need to work with
17+
// These are like "handles" that let us control the elements with JavaScript
18+
const carouselImg = document.getElementById("carousel-img"); // The <img> tag that shows the current picture
19+
const backwardBtn = document.getElementById("backward-btn"); // The "Back" button
20+
const forwardBtn = document.getElementById("forward-btn"); // The "Forward" button
21+
22+
// FUNCTION: updateImage - This updates the webpage to show the current image
23+
function updateImage() {
24+
// Set the image source to the current image in our array
25+
// images[currentImageIndex] gets the image path/URL at the current position
26+
// Example: if currentImageIndex = 2, then images[2] = "./assets/cute-cat-c.jpg"
27+
carouselImg.src = images[currentImageIndex];
28+
29+
// Update the alt text for accessibility (screen readers for visually impaired users)
30+
// This describes what the image shows
31+
carouselImg.alt = `Cat picture ${currentImageIndex + 1}`;
32+
// currentImageIndex + 1 because we want "Cat picture 1" instead of "Cat picture 0"
33+
}
34+
35+
// FUNCTION: showNextImage - This shows the next image when forward button is clicked
36+
function showNextImage() {
37+
// Move to the next index by adding 1 to currentImageIndex
38+
// Example: if current was 0, now it becomes 1
39+
currentImageIndex++;
40+
41+
// CHECK FOR LOOPING: If we've gone past the last image, go back to the first
42+
// images.length tells us how many images are in the array (in this case, 4)
43+
// If currentImageIndex becomes 4 or more, we've gone too far
44+
if (currentImageIndex >= images.length) {
45+
currentImageIndex = 0; // Reset to first image (index 0)
46+
}
47+
48+
// Now update the actual image on the webpage
49+
updateImage();
50+
}
51+
52+
// FUNCTION: showPreviousImage - This shows the previous image when back button is clicked
53+
function showPreviousImage() {
54+
// Move to the previous index by subtracting 1 from currentImageIndex
55+
// Example: if current was 2, now it becomes 1
56+
currentImageIndex--;
57+
58+
// CHECK FOR LOOPING: If we've gone before the first image, go to the last image
59+
// If currentImageIndex becomes negative (less than 0), we've gone too far back
60+
if (currentImageIndex < 0) {
61+
currentImageIndex = images.length - 1; // Go to last image (index 3 for 4 images)
62+
// images.length - 1 because arrays start at 0, so last index is length-1
63+
}
64+
65+
// Now update the actual image on the webpage
66+
updateImage();
67+
}
68+
69+
// SET UP BUTTON CLICK HANDLERS: Make the buttons actually work
70+
// When someone clicks the forward button, run the showNextImage function
71+
forwardBtn.addEventListener("click", showNextImage);
72+
73+
// When someone clicks the backward button, run the showPreviousImage function
74+
backwardBtn.addEventListener("click", showPreviousImage);
775

8-
// Write your code here
76+
// INITIALIZATION: The carousel is ready to go!
77+
// The first image automatically shows because:
78+
// 1. currentImageIndex starts at 0 (first image)
79+
// 2. Our HTML already has src="./assets/cute-cat-a.png" which is the first image
80+
// 3. When page loads, the browser shows that image immediately

0 commit comments

Comments
 (0)