igetstuckalot
igetstuckalot

Reputation: 237

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');
    }
}

Upvotes: 1

Views: 75

Answers (3)

Jobelle
Jobelle

Reputation: 2834

Try this

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', '300px');
        }
        if (xChestPosition == 6) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 7) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 8) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 9) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 10) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 11) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 12) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 13) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 14) {
            $('#chest').css('left', '300px');
        }
        if (xChestPosition == 15) {
            $('#chest').css('left', '300px');
        }
    }

Upvotes: 0

VioLoustic
VioLoustic

Reputation: 3

You'd better use:

$('#chest').css({"position": "absolute", "left": "300px"});

rather than use:

$('#chest').css('left','300');

Because, if you want to set left attributes, you must specify its position (absolute, fixed, relative). Your "if" statement can be executed well.

Upvotes: 0

Atticus
Atticus

Reputation: 6720

It's likely that you may need to specify the unit of measurement for the CSS position

$('#chess').css('left', '300px')

Upvotes: 1

Related Questions