ayyyee
ayyyee

Reputation: 5

Why is the result of my calculation sometimes undefined?

So I tried to create my own slideshow(s) with random image rotating. Sometimes the result of the random-number-calculation is undefined.

$(document).ready(function () {

    var slide = 2;  
    var si_array1 = [1, 2, 3];
    var si_nindex1 = 0;
    var si_index1 = 0;
    var si_array2 = [1, 2, 3];
    var si_nindex2 = 0;
    var si_index2 = 0;

    initSlider();
    function initSlider() {

        setupSlider();      
        function setupSlider() {

            if(slide === 1) {
                si_nindex1 = Math.floor(Math.random() * 10) % si_array1.length;
                si_index1 = (si_nindex1 === si_index1) ? si_nindex1 -1 : si_nindex1;
                updateSlider('#sl-one', 'sl-one', si_array1[si_index1]);        
            } else if(slide === 2) {
                si_nindex2 = Math.floor(Math.random() * 10) % si_array2.length;
                si_index2 = (si_nindex2 === si_index2) ? si_nindex2 -1 : si_nindex2;
                updateSlider('#sl-two', 'sl-two', si_array2[si_index2]);
            }

        }

        function updateNumber() {

            if(slide === 1) {
                slide = 2;                          
            } else if(slide === 2) {
                slide = 1;
            }   
            setTimeout(initSlider, 5000);

        }

        function updateSlider(sliderid, slider, img_nr) {

            $(sliderid).css('backgroundImage', 'url(/img/slides/' + slider + '/slide' + img_nr + '.jpg)');
            updateNumber();

        }

    }

});

Why does this happen, and how can I fix it? Thanks for your help.

Upvotes: 0

Views: 61

Answers (2)

Kabulan0lak
Kabulan0lak

Reputation: 2136

That is not the random number calculation that is undefined, but si_array1[si_index1]. When si_nindex1 is equal to 0 at the first loop, you set si_index1 to -1 and therefore you try to access si_array1[-1] which does not exists.

You can modify slightly your code to avoid this problem, for example by adding this line :

si_index1 = (si_index1 < 0) ? si_index1 + 2 : si_index1;

So your code becomes:

$(document).ready(function () {
var slide = 2;  
var si_array1 = [1, 2, 3];
var si_nindex1 = 0;
var si_index1 = 0;
var si_array2 = [1, 2, 3];
var si_nindex2 = 0;
var si_index2 = 0;

initSlider();
function initSlider() {

    setupSlider();      
    function setupSlider() {

        if(slide === 1) {
            si_nindex1 = Math.floor(Math.random() * 10) % si_array1.length;
            si_index1 = (si_nindex1 === si_index1) ? si_nindex1 -1 : si_nindex1;

            si_index1 = (si_index1 < 0) ? si_index1 + 2 : si_index1; //Code added

            updateSlider('#sl-one', 'sl-one', si_array1[si_index1]);        
        } else if(slide === 2) {
            si_nindex2 = Math.floor(Math.random() * 10) % si_array2.length;
            si_index2 = (si_nindex2 === si_index2) ? si_nindex2 -1 : si_nindex2;

            si_index2 = (si_index2 < 0) ? si_index2 + 2 : si_index2; //Code added

            updateSlider('#sl-two', 'sl-two', si_array2[si_index2]);
        }

    }

    function updateNumber() {

        if(slide === 1) {
            slide = 2;                          
        } else if(slide === 2) {
            slide = 1;
        }   
        setTimeout(initSlider, 5000);

    }

    function updateSlider(sliderid, slider, img_nr) {

        $(sliderid).css('backgroundImage', 'url(/img/slides/' + slider + '/slide' + img_nr + '.jpg)');
        updateNumber();

    }

}

});

Upvotes: 0

RobG
RobG

Reputation: 147393

I expect that it isn't the random number calculation that is undefined, but the result of:

si_array1[si_index1]

Where you have:

si_nindex1 = Math.floor(Math.random() * 10) % si_array1.length;
si_index1 = (si_nindex1 === si_index1) ? si_nindex1 -1 : si_nindex1;

then if si_nindex1 and si_index1 are both 0, then si_index1 will be set to -1 and:

si_array1[si_index1]

will return undefined.

Upvotes: 1

Related Questions