0

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');
    }
}
3
  • 3
    left work only on absolute, relative and fixed positioned elements. Commented Feb 18, 2016 at 3:08
  • 3
    by the way switch case statement would be nicer :) Commented Feb 18, 2016 at 3:11
  • @igetstuckalot by the way what about if( xChestPosition>=5 && xChestPosition<=15){$('#chest').css('left', '300px'); Commented Feb 18, 2016 at 4:05

3 Answers 3

1

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

$('#chess').css('left', '300px')
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.