Can somebody explain to me this javascript code please?

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  if (n > slides.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";  
  }
  
  slides[slideIndex-1].style.display = "block";  
}
   

its not great code in my opinion, but here goes:

function showSlides(n) {
  var i;
  // select all elements with class mySlides
  var slides = document.getElementsByClassName("mySlides");
  // if passed param n is greater than number of slides, set index to 1
  // note: slideIndex must be declared before this point to avoid creating an accidental global variable
  if (n > slides.length) {slideIndex = 1}    
  // if passed param n is less than 1, set index to total number of slides
  if (n < 1) {slideIndex = slides.length}
  // loop over slides setting the display to none, essentially hiding them
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";  
  }
  // display the slide at current slide index, subtract one to make the number index 0
  slides[slideIndex-1].style.display = "block";  
}
   
2 Likes

thank you so much my friend good explication

1 Like