1-
21// Write your code here
2+
33// ARRAY OF IMAGES: This stores the file paths or URLs of all images in our carousel
44// We need at least 4 images as specified in the requirements
55const images = [
@@ -13,57 +13,103 @@ const images = [
1313// We start at 0 because array indexes start at 0 (0 = first image, 1 = second image, etc.)
1414let currentImageIndex = 0 ;
1515
16+ // AUTO-PLAY VARIABLES
17+ let slideInterval ; // This will store our timer
18+ let isAutoPlaying = false ; // Track if auto-play is running
19+
1620// GET HTML ELEMENTS: We find the HTML elements we need to work with
1721// 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
22+ const carouselImg = document . getElementById ( "carousel-img" ) ; // The <img> tag that shows the current picture
23+ const backwardBtn = document . getElementById ( "backward-btn" ) ; // The "Back" button
24+ const forwardBtn = document . getElementById ( "forward-btn" ) ; // The "Forward" button
2125
2226// FUNCTION: updateImage - This updates the webpage to show the current image
2327function 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"
28+ // Set the image source to the current image in our array
29+ // images[currentImageIndex] gets the image path/URL at the current position
30+ // Example: if currentImageIndex = 2, then images[2] = "./assets/cute-cat-c.jpg"
31+
32+ carouselImg . src = images [ currentImageIndex ] ;
33+
34+ // Update the alt text for accessibility (screen readers for visually impaired users)
35+ // This describes what the image shows
36+ carouselImg . alt = `Cat picture ${ currentImageIndex + 1 } ` ;
37+ // currentImageIndex + 1 because we want "Cat picture 1" instead of "Cat picture 0"
3338}
3439
3540// FUNCTION: showNextImage - This shows the next image when forward button is clicked
3641function 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 ( ) ;
42+ // Move to the next index by adding 1 to currentImageIndex
43+ // Example: if current was 0, now it becomes 1
44+ currentImageIndex ++ ;
45+
46+ // CHECK FOR LOOPING: If we've gone past the last image, go back to the first
47+ // images.length tells us how many images are in the array (in this case, 4)
48+ // If currentImageIndex becomes 4 or more, we've gone too far
49+ if ( currentImageIndex >= images . length ) {
50+ currentImageIndex = 0 ; // Reset to first image (index 0)
51+ }
52+
53+ // Now update the actual image on the webpage
54+ updateImage ( ) ;
5055}
5156
52- // FUNCTION: showPreviousImage - This shows the previous image when back button is clicked
57+ // FUNCTION: showPreviousImage - This shows the previous image when back button is clicked
5358function 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 ( ) ;
59+ // Move to the previous index by subtracting 1 from currentImageIndex
60+ // Example: if current was 2, now it becomes 1
61+ currentImageIndex -- ;
62+
63+ // CHECK FOR LOOPING: If we've gone before the first image, go to the last image
64+ // If currentImageIndex becomes negative (less than 0), we've gone too far back
65+ if ( currentImageIndex < 0 ) {
66+ currentImageIndex = images . length - 1 ; // Go to last image (index 3 for 4 images)
67+ // images.length - 1 because arrays start at 0, so last index is length-1
68+ }
69+
70+ // Now update the actual image on the webpage
71+ updateImage ( ) ;
72+ }
73+
74+ // NEW FUNCTIONS FOR AUTO-PLAY
75+
76+ // FUNCTION: startAutoForward - Start automatically moving forward
77+ function startAutoForward ( ) {
78+ // First, stop any existing auto-play
79+ stopAutoPlay ( ) ;
80+
81+ // Set up a timer that calls showNextImage every 3000 milliseconds (3 seconds)
82+ slideInterval = setInterval ( showNextImage , 3000 ) ;
83+
84+ // Mark that auto-play is running
85+ isAutoPlaying = true ;
86+ console . log ( "Auto-Forward started" ) ;
87+ }
88+
89+ // FUNCTION: startAutoBack - Start automatically moving backward
90+ function startAutoBack ( ) {
91+ // First, stop any existing auto-play
92+ stopAutoPlay ( ) ;
93+
94+ // Set up a timer that calls showPreviousImage every 3000 milliseconds (3 seconds)
95+ slideInterval = setInterval ( showPreviousImage , 3000 ) ;
96+
97+ // Mark that auto-play is running
98+ isAutoPlaying = true ;
99+ console . log ( "Auto-Back started" ) ;
100+ }
101+
102+ // FUNCTION: stopAutoPlay - Stop the automatic slideshow
103+ function stopAutoPlay ( ) {
104+ // Check if there's a timer running
105+ if ( slideInterval ) {
106+ // Clear (stop) the timer
107+ clearInterval ( slideInterval ) ;
108+
109+ // Mark that auto-play is stopped
110+ isAutoPlaying = false ;
111+ console . log ( "Auto-Play stopped" ) ;
112+ }
67113}
68114
69115// SET UP BUTTON CLICK HANDLERS: Make the buttons actually work
@@ -73,8 +119,21 @@ forwardBtn.addEventListener("click", showNextImage);
73119// When someone clicks the backward button, run the showPreviousImage function
74120backwardBtn . addEventListener ( "click" , showPreviousImage ) ;
75121
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
122+ // GET THE NEW AUTO-PLAY BUTTONS
123+ const autoForwardBtn = document . getElementById ( "auto-forward-btn" ) ;
124+ const autoBackBtn = document . getElementById ( "auto-back-btn" ) ;
125+ const stopBtn = document . getElementById ( "stop-btn" ) ;
126+
127+ // Add click handlers for the new auto-play buttons
128+ if ( autoForwardBtn ) {
129+ autoForwardBtn . addEventListener ( "click" , startAutoForward ) ;
130+ }
131+
132+ if ( autoBackBtn ) {
133+ autoBackBtn . addEventListener ( "click" , startAutoBack ) ;
134+ }
135+
136+ if ( stopBtn ) {
137+ stopBtn . addEventListener ( "click" , stopAutoPlay ) ;
138+ }
139+
0 commit comments