2017-05-14 17 views
1

生成された乱数を呼び出して、乱数に条件文を設定しようとしていますが、呼び出す方法がわかりません!誰かが助けてくれますか?変数の中から関数を呼び出す

// This is the random number generator 
    $(document).ready(function() { 
     var minNumber = 0; 
     var maxNumber = 9; 
     var randomValue = function(){ 
      return Math.floor(Math.random() * maxNumber); 
     } 
     $("#random-number").html(randomValue().toString()); 
     // I need to return this value to the "myGuess class and print it on the screen" 
     console.log(randomValue().toString()); 
    }); 
    // This ends the random number generator 


    // If you click a number and it equals the random number 
    $(".number").on("click", function(){ 
     var value = $(this).val(); 
     if($(".guessesLeftVar").text() === function.randomValue){ 
      $(".winCount").text($(".winCount").text()+value); 
     } 
     else if($(".guessesLeftVar").text() != function.randomValue){ 
      $(".lossCount").text($(".lossCount").text()+value); 
     } 
    }); 

答えて

0

あなたはdocumnet readyにmove clickイベントが必要です。 clickイベントからrandomValue関数を呼び出します。ここで

$(document).ready(function() { 
    var minNumber = 0; 
    var maxNumber = 9; 
    var randomValue = function(){ 
     return Math.floor(Math.random() * maxNumber); 
    } 

    $(".number").on("click", function(){ 

     var random = randomValue(); 

     $("#random-number").html(random); 

     var value = $(this).val(); 
     if($(".guessesLeftVar").text() === random){ 
      $(".winCount").text($(".winCount").text()+value); 
     } 
     else if($(".guessesLeftVar").text() != random){ 
      $(".lossCount").text($(".lossCount").text()+value); 
     } 
    }); 
}); 
0

を以下のようにコードを変更することは、より組織的に何をしたいあなたができる一つの方法です:

// This ends the random number generator 
 

 
// loop over our object displaying each value 
 
function displayValues(obj) { 
 
    $.each(obj, function(key, value) { 
 
    $('#' + key).text(value); 
 
    }) 
 
} 
 
// returns random number in the provided range 
 
function getRandomValue(minNumber, maxNumber) { 
 
    return Math.floor(Math.random() * maxNumber); 
 
} 
 

 
// once the doc loads, do all your event binding 
 
$(document).ready(function() { 
 
    
 
    // creat object to hold all our vars 
 
    var guesses = { 
 
     remaining: 5, 
 
     wins: 0, 
 
     losses: 0, 
 
     random: getRandomValue(0,9), 
 
    } 
 
    
 
    // show the initial values 
 
    displayValues(guesses) 
 

 
    $(".number").on("click", function() { 
 
    var value = $(this).val(); 
 
    if (Number(value) === guesses.random) { 
 
     guesses.wins++; 
 
     // get a new random number 
 
     guesses.random = getRandomValue(0,9) 
 
    } else { 
 
     guesses.losses++; 
 
     guesses.remaining--; 
 
     if (guesses.remaining == 0) { 
 
     // no guesses left, end game 
 
     $('#numbers').replaceWith("<h1>game over</h1>"); 
 
     } 
 
    } 
 
    // show the new values 
 
    displayValues(guesses) 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
wins: 
 
<div id="wins"></div> 
 
<br> losses: 
 
<div id="losses"></div> 
 
<br> remaining guesses: 
 
<div id="remaining"></div> 
 
<br> random number (remove in actual game): 
 
<div id="random"></div> 
 
<br> 
 

 

 
<div id="numbers"> 
 
<button class="number" value="0">0</button> 
 
<button class="number" value="1">1</button> 
 
<button class="number" value="2">2</button> 
 
<button class="number" value="3">3</button> 
 
<button class="number" value="4">4</button> 
 
<button class="number" value="5">5</button> 
 
<button class="number" value="6">6</button> 
 
<button class="number" value="7">7</button> 
 
<button class="number" value="8">8</button> 
 
<button class="number" value="9">9</button> 
 

 
</div>

関連する問題