mercredi 30 août 2017

Getting the sum of randomly generated numbers from 4 sources (jquery)

New to coding. I have 4 buttons that will generate a random number. I need those numbers to continue to adding up every time one of the buttons is clicked. If I click button 1 then button 2, I need them to add. If I click button 2 twice, I need it to perform the random number function twice, then add both of those random numbers.

function getRandomArbitrary(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; 
console.log()
}

$('#crystal1').click(function() {
   crystal1Num = getRandomArbitrary(1,25);
   $("#crystalValues").html(crystal1Num);
   crystalValue = parseInt($("#crystalValues").html());
   console.log(crystalValue)
})

$('#crystal2').click(function() {
   crystal2Num = getRandomArbitrary(26,50);
   $("#crystalValues").html(crystal2Num);
   crystalValue = parseInt($("#crystalValues").html());
   console.log(crystalValue)
})

$('#crystal3').click(function() {
   crystal3Num = getRandomArbitrary(51,75);
   $("#crystalValues").html(crystal3Num);
   crystalValue = parseInt($("#crystalValues").html());
   console.log(crystalValue)
})

$('#crystal4').click(function() {
   crystal4Num = getRandomArbitrary(76,100);
   $("#crystalValues").html(crystal4Num);
   crystalValue = parseInt($("#crystalValues").html());
   console.log(crystalValue)
})


<!-- html is here -->
<body>
    <div class="container">
        <div class="jumbotron text-center">
            <div class="randNumContainer">
                <button id="generatebtn" class="btn btn-default btn-lg 
btn-primary">
                Generate Random Number
                </button>
                <h1 id="numberRand">0</h1>
            </div>
            <div class="valuesContainer">
                <h1 id="crystalValues">0</h1>
            </div>
            <div class="btnContainer container text-center">
                <button id="crystal1" class="btn btn-default btn-lg 
                 btn-primary crystalBtn">
                Crystal 1
                </button>
                <button id="crystal2" class="btn btn-default btn-lg 
                 btn-primary crystalBtn">
                Crystal 2
                </button>
                <button id="crystal3" class="btn btn-default btn-lg 
                 btn-primary crystalBtn">
                Crystal 3
                </button>
                <button id="crystal4" class="btn btn-default btn-lg 
                 btn-primary crystalBtn">
                Crystal 4
                </button>
             </div>
         </div>
     </div>
   </body>




Aucun commentaire:

Enregistrer un commentaire