I am starting from scratch with web development. On my page, there is an image carousel with two buttons for navigating between different images. When I use Brackets live preview to test my website, I encounter the following error message.
Uncaught TypeError: Cannot read properties of undefined (reading 'style') at showSlide (script.js:15:28) at script.js:24:5
Below is the code snippet:
var slideIndex = 0;
var slides = document.getElementsByClassName("mySlides");
// Function to display the current slide
function showSlide(n) {
// Wrap around to the first slide if exceeding the number of slides
slideIndex = (n + slides.length) % slides.length;
// Hide all slides
for (var i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
// Display the current slide
slides[slideIndex].style.display = "block";
}
// Function to navigate to the previous or next slide
function plusSlides(n) {
showSlide(slideIndex + n);
}
// Display the first slide
showSlide(slideIndex);
The images are not appearing on the screen due to a JavaScript malfunction.