0

I’ve started my first own JS project. In the end, I would like to have my own slotmachine. By now, I’ve the problem, that I don’t know how to restart my program or just the function for generating again and again the new random values. This is my code until now:

var randomX1 = Math.ceil(Math.random() * 10 );
var randomX2 = Math.ceil(Math.random() * 10 );
var randomX3 = Math.ceil(Math.random() * 10 );

function randomClickX() {
    document.getElementById("randomX1").innerHTML = randomX1;
    document.getElementById("randomX2").innerHTML = randomX2;
    document.getElementById("randomX3").innerHTML = randomX3;

    var ausgabe = "Play again";
    if (randomX1 === randomX2) {
    if(randomX2 === randomX3) {
        var ausgabe = "Jackpot";
        }
    }


    var chance = ausgabe;
    function clickChance() {
        document.getElementById("chance").innerHTML = chance;
    }
    document.getElementById('spanId').innerHTML = chance;
};
.slot-container {
    border: 5px solid red;
    vertical-align: middle;
    font-size: 50px;
    font-family: Leelawadee UI Semilight, Calibri, Arial;
    width: 90%;
    margin: auto;
    height: 0;            
    padding-bottom: 30%;   
}


.slot {
    border: 3px solid green;
    height: 0;
    padding-bottom: 30%;
    width: 32%;
    margin-right: 1%;
    margin-top: 1%;
    margin-bottom: 1%;
    float: left; 
    box-sizing: border-box;
}

#slot1 {
    margin-left: 1%;
}


h1 {
    color: #FC3FD3;
    text-align: center;
    font-family: Century Gothic;
    font-size: 5em;
    height: 50px;
}

p {
    text-align: center;
    vertical-align: middle;
    justify-content: center;
}


button {
    position: relative;
    font-size:20px;
    display: block;
    background-color: white;
    color: black;
    border: 2px solid green;
    width: 90%;
    position: middle;
    margin-left: auto;
    margin-right: auto;
    margin-top: 10px;
    margin-bottom: 10px;


}

button:hover {
    background-color: green;
}

button.spin-button {
    color: blue;
    position: absolute;
    height: 20%;
}



.answer {
    font-family: Century Gothic;
    font-size: 20px;
    color: red;
    text-align: center;
    margin-top: 10px;
}

ul.menubar {
    list-style-type: none; 
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #008000;
}

ul.menubar li {
    float: left; 
}

ul.menubar li a {
    color: black;
    display: inline-block;
    text-align: center;
    padding:15px 20px;
    text-decoration: none; 
    transition: 0.3s; 
    font-family: Century Gothic;
}

ul.menubar li a:hover {
    background-color: #00A300;
}
<!DOCTYPE HTML>

<html>

<head>
  <title>Slotmachine</title>
  <script type="text/javascript" src="#.js"></script>
  <link type="text/css" rel="stylesheet" href="#.css" />
</head>

<body>
    <ul class="menubar" id=topmenubar>
        <li><a href="#linkHome">Home</a></li> <!-- bedeutet '#' gleicher Link + Ergänzung?-->
        <li><a href="#linkContact">Contact</a></a></li>
        <li><a href="#linkAbout">About</a></li>
    </ul>


    <h1>1-10</h1>


    <button type="button" id="spin-button" class="button" onClick="randomClickX()">SPIN</button>
    <div class="slot-container">
        <div id="slot1" class="slot">
            <p> <!-- Your random number: --> <a id="randomX1">0</a></p>
        </div>
        <div id="slot2" class="slot">
            <p> <!-- Your random number: --> <a id="randomX2">0</a></p>
        </div>
        <div id="slot3" class="slot">
            <p> <!-- Your random number: --> <a id="randomX3">0</a></p>
        </div>
        </div>
    </div>


    <div class="answer" id="spanId">Test #1<a id="spanId"> </div>


</body>

</html>

As you see, I've to reload the HTML file to click the SPIN-button again to have new randoms. I think the point is to create a big comprehensive function and call it when the SPIN function ends. But I don't now how to do that ... What is the missing step, to have as many values as the user wishs to have?

Thanks, Jonas

Justin Herter
  • 590
  • 4
  • 17
Jonas
  • 111
  • 2
  • 13

1 Answers1

1

Simply move the random numbers being generated into the click method.

function randomClickX() {
    var randomX1 = Math.ceil(Math.random() * 10 );
    var randomX2 = Math.ceil(Math.random() * 10 );
    var randomX3 = Math.ceil(Math.random() * 10 );

    document.getElementById("randomX1").innerHTML = randomX1;
    document.getElementById("randomX2").innerHTML = randomX2;
    document.getElementById("randomX3").innerHTML = randomX3;

    var ausgabe = "Play again";
    if (randomX1 === randomX2) {
    if(randomX2 === randomX3) {
        var ausgabe = "Jackpot";
        }
    }


    var chance = ausgabe;
    function clickChance() {
        document.getElementById("chance").innerHTML = chance;
    }
    document.getElementById('spanId').innerHTML = chance;
};
.slot-container {
    border: 5px solid red;
    vertical-align: middle;
    font-size: 50px;
    font-family: Leelawadee UI Semilight, Calibri, Arial;
    width: 90%;
    margin: auto;
    height: 0;            
    padding-bottom: 30%;   
}


.slot {
    border: 3px solid green;
    height: 0;
    padding-bottom: 30%;
    width: 32%;
    margin-right: 1%;
    margin-top: 1%;
    margin-bottom: 1%;
    float: left; 
    box-sizing: border-box;
}

#slot1 {
    margin-left: 1%;
}


h1 {
    color: #FC3FD3;
    text-align: center;
    font-family: Century Gothic;
    font-size: 5em;
    height: 50px;
}

p {
    text-align: center;
    vertical-align: middle;
    justify-content: center;
}


button {
    position: relative;
    font-size:20px;
    display: block;
    background-color: white;
    color: black;
    border: 2px solid green;
    width: 90%;
    position: middle;
    margin-left: auto;
    margin-right: auto;
    margin-top: 10px;
    margin-bottom: 10px;


}

button:hover {
    background-color: green;
}

button.spin-button {
    color: blue;
    position: absolute;
    height: 20%;
}



.answer {
    font-family: Century Gothic;
    font-size: 20px;
    color: red;
    text-align: center;
    margin-top: 10px;
}

ul.menubar {
    list-style-type: none; 
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #008000;
}

ul.menubar li {
    float: left; 
}

ul.menubar li a {
    color: black;
    display: inline-block;
    text-align: center;
    padding:15px 20px;
    text-decoration: none; 
    transition: 0.3s; 
    font-family: Century Gothic;
}

ul.menubar li a:hover {
    background-color: #00A300;
}
<!DOCTYPE HTML>

<html>

<head>
  <title>Slotmachine</title>
  <script type="text/javascript" src="#.js"></script>
  <link type="text/css" rel="stylesheet" href="#.css" />
</head>

<body>
    <ul class="menubar" id=topmenubar>
        <li><a href="#linkHome">Home</a></li> <!-- bedeutet '#' gleicher Link + Ergänzung?-->
        <li><a href="#linkContact">Contact</a></a></li>
        <li><a href="#linkAbout">About</a></li>
    </ul>


    <h1>1-10</h1>


    <button type="button" id="spin-button" class="button" onClick="randomClickX()">SPIN</button>
    <div class="slot-container">
        <div id="slot1" class="slot">
            <p> <!-- Your random number: --> <a id="randomX1">0</a></p>
        </div>
        <div id="slot2" class="slot">
            <p> <!-- Your random number: --> <a id="randomX2">0</a></p>
        </div>
        <div id="slot3" class="slot">
            <p> <!-- Your random number: --> <a id="randomX3">0</a></p>
        </div>
        </div>
    </div>


    <div class="answer" id="spanId">Test #1<a id="spanId"> </div>


</body>

</html>
Justin Herter
  • 590
  • 4
  • 17