mercredi 17 février 2016

javascript if statement not working

I have a random number generator which generates numbers between 5 and 15. I want to be able to change the position of the div 'chest' depending on which number the computer chooses. (all set to 300 because it's being tested.) I do not know why it is not working. (please bear in mind that the div is definitely called chest, and i have a css statement that includes 'left'. Furthermore, the function is definitely being called, and is generating a number.) Any help is appreciated,

Here is my code:

var xChestPosition;
var yChestPosition;
function randomNumberForChest(firstNum, secondNum) {
    xChestPosition = Math.floor(Math.random() * secondNum) + firstNum;
    yChestPosition = Math.floor(Math.random() * secondNum) + firstNum;
    alert(xChestPosition);
    if(xChestPosition==5) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==6) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==7) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==8) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==9) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==10) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==11) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==12) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==13) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==14) {
        $('#chest').css('left','300');
    }
    if(xChestPosition==15) {
        $('#chest').css('left','300');
    }
}

Aucun commentaire:

Enregistrer un commentaire