私は非常にプログラミングに新しいので、いくつかの助けを得ることはいいです。イベントをトリガーするときに変数が更新されないのはなぜですか?
ボタンを押したときに変数が更新されないのはなぜですか?
HTML:
<h1>NIM</h1>
<p>Welcome to a simple edition of the game NIM</p>
<table>
<tr>
<th>You</th>
<th>Left</th>
<th>PC</th>
</tr>
<tr>
<td><p id="pl" class="nr">0</p></td>
<td><p id="a" class="nr">25</p></td>
<td><p id="pc" class="nr">0</p></td>
</tr>
</table>
<br>
<p>How many do you want to pull?</p>
<input type="number" id="val" value="1" min="1" max="3">
<button id="turn">Trekk</button>
はJavaScript:
私はおそらくC++に精通してい$('#turn').click(function() {
var val = parseInt($('#val').val()),
player = parseInt($('#pl').html()),
pc = parseInt($('#pc').html()),
total = parseInt($('#a').html());
console.log(val);
console.log(total);
do {
switch (val) {
case 1:
total -= 4;
player += 1;
pc += 3;
break;
case 'b2':
total -= 4;
player += 2;
pc += 2;
break;
case 'b3':
total -= 4;
player += 3;
pc += 1;
break;
}
} while (total > 1);});
が、これは私がジャバスクリプト/ jqueryの内doループを試してみてください最初の時間です。
このコードは実行されていますか? –
私はここで[fiddle](https://jsfiddle.net/Rudauke/ssw9w230/)を作成しました。変数が更新されています。テキストは更新されませんが、それはどこでも更新しないからです。 –
あなたのコードは動作しています。変数が変化します。 'do'ループの後に' console.log() 'を使用してください –