Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to check for length === 0 to restart the slides?

I'm following the Codecademy "Make an interactive website" course. I don't have an error, but I had a question about something I don't understand: the if-statement used in a slide thing thing thing.

if (nextSlide.length === 0) {
            nextSlide = $('.slide').first();
            nextDot = $('.dot').first();
        };

This if-statement is for that after the last slide it will go to the first slide. Why is it nextSlide.length === 0?

jQuery:

var main = function() {
    $('.dropdown-toggle').click(function() {
        $('.dropdown-menu').toggle();
    });
    $('.arrow-next').click(function() {
        var currentSlide = $('.active-slide');
        var nextSlide = currentSlide.next();

        var currentDot = $('.active-dot');
        var nextDot = currentDot.next();

        if (nextSlide.length === 0) {
            nextSlide = $('.slide').first();
            nextDot = $('.dot').first();
        };

        currentSlide.fadeOut(600).removeClass('active-slide');
        nextSlide.fadeIn(600).addClass('active-slide');

        currentDot.removeClass('active-dot');
        nextDot.addClass('active-dot');
    });
};

$(document).ready(main);
like image 424
Coder Avatar asked Apr 29 '15 13:04

Coder


1 Answers

The if statement is there to do a check as to if there is a slide after the current slide or if it should revert to the first slide.

nextSlide.length will return a number to represent if the next slide exists, else it will return 0.

.length API Docs: Here

like image 136
Stewartside Avatar answered Sep 25 '22 01:09

Stewartside