Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How can I use this jQuery if/else statement correctly?

Can anyone tell me what I'm doing wrong in this script? I'm trying to get the slideshow to loop around when there are no more images left. So it should be something like - " If the img's index is less than 3, keep going. If the not, then go back to the beginning img. Any help would be great!

Forgot to link my Codepen that I'm using!!! http://codepen.io/JHicks/pen/FyLes

1 Answer

The problem is that the nextPhoto "falls off" on the statement currentPhoto.next() when the index is greater than the index (since is going beyond the the third image). So you should use something like:

  if(index < 3) {
     currentPhoto.fadeOut("slow").removeClass('show');
     currentPhoto.next().slideUp().addClass('show');
  } else {
      currentPhoto.fadeOut("slow");
      currentPhoto.siblings("img").first().slideDown().addClass("show");
 } 

I also added "img" in the sibligs() function so I don't get one undefined value for currentPhoto.