This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Dejan Gajšek
over 8 years

Why this code works? (if statement quesiton)

This might be a super easy answer and I just need for someone to explain it again.

So do code runs correctly if it’s written like that (just a part of the code):

    if(nextSlide.length == 0) {
    nextSlide = $('.slide').first();
    nextDot = $('.dot').first();
    }
    
    currentDot.removeClass('active-dot');
    nextDot.addClass('active-dot');
    
    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');

However it won’t work correctly if the if statement is written after adding and removing classes from the slide and dots. Why?

Thanks.

Answer 55d7c5b9e39efe2a1400037f

0 votes

Permalink

We have to be sure that nextDot is the correct one before we add the class, so it must be set first.

points
Submitted by Roy
over 8 years

1 comments

Dejan Gajšek over 8 years

Oh right. Makes perfect sense. Thanks.