2017-08-13 13 views
-1

jQueryでクリックして乱数を生成しています。この記事の最後に、私が作業しているコードを見ることができます。javascriptで乱数を自分自身に追加する

新しい乱数が生成されるたびに、古い乱数に、totalRandomNumbersという変数で追加される方法はありますか?

私は3つの変数すべてに対してしたいですrandomrandom_0random_1です。あなたのJavaScriptの開始時

$("#reset").click(function(){ 
 
      var random = Math.floor(Math.random() * 12); 
 
      var random_0 = Math.floor(Math.random() * 12); 
 
      var random_1 = Math.floor(Math.random() * 12); 
 

 
      $("#push0").html(random); 
 
      $("#push1").html(random_0); 
 
      $("#push2").html(random_1); 
 
      $('input[name="hamPush"]').val($("#push0").html()); 
 
      $('input[name="zikPush"]').val($("#push1").html()); 
 
      $('input[name="musPush"]').val($("#push2").html());  
 
})

+1

もあなたのコードを教えてください? – kukkuz

+0

乱数の値を追加したか、世代数を保持していますか? – wrangler

+0

世代数ではなく、生成された数を追加したいと思います。 –

答えて

0

、変数ます

var totalRandomNumbers = 0; 

そして、あなたはあなたの乱数を生成し、それを変数に追加します。あなたはgetアクセサに何かを行うことができます:-)

var _totalRandomNumbers = 0; 
 

 
Object.defineProperty(window, "totalRandomNumbers", { 
 
    get: function() { return _totalRandomNumbers += Math.random(); } 
 
}); 
 

 
console.log(totalRandomNumbers); 
 
console.log(totalRandomNumbers);

ので、古い値を格納:ちょうどブラウザコンソールで試してみました

totalRandomNumbers += (code to generate random number) 
0
var randomSum = 0; 
function getRandomInt(min, max) { 
    min = Math.ceil(min); 
    max = Math.floor(max); 
    return Math.floor(Math.random() * (max - min)) + min; 
}; 

function sumRandom() { 
randomSum += getRandomInt(1, 100) 
}; 

$('.genRandomButton').on('click', function() { 
    console.log(randomSum) 
} 
-2

関連する問題