dimanche 28 octobre 2018

Function wont switch out images when trying to use a random number

im working on a slot machine game where i take 3 random numbers and assign them to different images. However i can not get my images to diplay when you click the button to play the game. I have the stock images there but when you click SPIN it should put up some new images based on the random number it gave. If you could help me figure out how to get the new images to display that would be great. (sorry if its a silly mistake, im new to coding)

these are the image boxes:

< div class="SlotDiv" id="div_1">< image id="slot_1" src="images/lemon.jpg">< /image>< /div> < div class="SlotDiv" id="div_2">< image id="slot_2" src="images/cherry.jpg">< /image>< /div> < div class="SlotDiv" id="div_3">< image id="slot_3" src="images/bar.jpg">< /image>< /div>

(there arnt spaces in the front of these in the real code, they wont appear in the thread if i dont put the spaces for some reason)

<html>

Slots

    <link href="../style.css" type="text/css" rel="stylesheet">

<script type="text/javascript" src="random.js">

    function spinslots()
        {
            var lemon = document.getElementById('lemon.jpg');
            var donut = document.getElementById('donut.jpg');
            var cherry = document.getElementById('cherry.jpg');
            var bar = document.getElementById('bar.jpg');

            var random_1;
                random_1= Math.floor((Math.random()*4 )+ 1);
            var random_2;
                random_2 = Math.floor((Math.random()*4 )+ 1);
            var random_3;
                random_3 = Math.floor((Math.random()*4 )+ 1);

            if (random_1 == 1)
                {
                    document.getElementById("slot_1").src = "lemon.jpg";
                }
            if (random_1 == 2)
                {
                    document.getElementById("slot_1").src = "donut.jpg";
                }
            if (random_1 == 3)
                {
                    document.getElementById("slot_1").src = "cherry.jpg";
                }
            if (random_1 == 4)
                {
                    document.getElementById("slot_1").src = "bar.jpg";
                }


            if (random_2 == 1)
                {
                    document.getElementById("slot_2").src = "lemon.jpg";
                }
            if (random_2 == 2)
                {
                    document.getElementById("slot_2").src = "donut.jpg";
                }
            if (random_2 == 3)
                {
                    document.getElementById("slot_2").src = "cherry.jpg";
                }
            if (random_2 == 4)
                {
                    document.getElementById("slot_2").src = "bar.jpg";
                }


            if (random_3 == 1)
                {
                    document.getElementById("slot_3").src = "lemon.jpg";
                }
            if (random_3 == 2)
                {
                    document.getElementById("slot_3").src = "donut.jpg";
                }
            if (random_3 == 3)
                {
                    document.getElementById("slot_3").src = "cherry.jpg";
                }
            if (random_3 == 4)
                {
                    document.getElementById("slot_3").src = "bar.jpg";
                }

                if (random_1 == random_2 == random_3)
                    {
                        alert("Congradulations, you won!");

                    }


        }

</script>

Test your luck! Click the SPIN button

<p><button value="Spin" onclick="spinslots();"> SPIN </button></p>

<p>Credits: <div class="OutputBox" type="numeric" id="Credits" size="10">20</div></p>




Aucun commentaire:

Enregistrer un commentaire