2

私が作っているコイン投げプログラムのループがあります。問題は、それが早く終了しているように見えることです。見てください。

$(function() {
    $('#rollDice').click(function() {

        var e = document.getElementById("diceSides");
        var diceSides = e.options[e.selectedIndex].text;
        var diceRolls = document.getElementById('rollCount').value;

        if (diceRolls.match(/^[\d]*$/ )) {      
            if (diceRolls == "")  {
                alert ("Please fill out all forms then try again.");

            } else {

                $('#diceRollContainer').slideDown('slow');

                for (i=0;i<diceRolls;i++) {
                    var randNum = Math.floor(Math.random()*diceSides)+1;
                    var rolls = ("You rolled a " + diceSides + " sided die " + diceRolls + " times, and got the numbers ");

                    rollMinOne = rolls - 1;
                    if (i == rollMinOne) {
                        var rolls = (rolls + randNum + ".");
                    }
                    var rolls = (rolls + randNum + ", ");

                }
                alert (rolls);
            }
        } else {
            alert ("Make sure you only enter numbers and no spaces, then try again.");
        }
    });
});

問題は、for ループが完了するように見える前に、プログラムがロールを警告していることです。なぜこれを行うのですか?

4

2 に答える 2

2

そのコードにはいくつかのバグがありますが、表示されている動作を説明するものはrolls、ループを介して毎回の値を初期文字列にリセットすることです。

その行を外に移動すると、より近い値が得られますが、意図したように ではなくrollsMinOneからも計算されます (これが、適切な名前を選択することが非常に重要な理由です)。つまり、if ステートメントは決して真ではありません (文字列マイナス数値は「数値ではない」という値であり、何にも等しくありません[それ自体でも!])。rollsdiceRollsNaN

次に、(スタイルやデザインではなく) 唯一の機能上の問題は、ピリオドで既に追加した場合でも、最後にコンマを付けて値を追加することです。

すべてを一緒に入れて:

    var rolls = ("You rolled a " + diceSides + " sided die " + diceRolls + " times, and got the numbers ");
    for (i=0;i<diceRolls;i++) {
        var randNum = Math.floor(Math.random()*diceSides)+1;

        rollMinOne = diceRolls - 1;
        if (i == rollMinOne) {
            rolls = (rolls + randNum + ".");
        } else {
            rolls = (rolls + randNum + ", ");
        }

他の回答が言及しているように、同じ結果を得るためのより簡単で迅速な方法がありますが、コードが機能しない理由を理解することが重要だと感じています。

于 2012-04-12T03:50:01.453 に答える
0

私は退屈してあなたのコードを実装しました。これは最小限のテストで動作するようです

<script>
    $(function() {
        $('#rollDice').click(function() {

            var diceSides = $('#dice-sides').val();
            var diceRolls = $('#roll-count').val();

            if (diceRolls.match(/^[\d]*$/ )) {      
                if (diceRolls == "")  {
                    alert ("Please fill out all forms then try again.");

                } else {
                    $('#output').text(
                        "You rolled a " + diceSides + 
                        " sided die " + diceRolls + 
                        " times, and got the numbers ");

                    for (i=0; i<diceRolls; i++) {

                        var randNum = Math.floor(Math.random()*diceSides)+1;

                        $('#output').append(randNum);
                    }
                }
            } else {
                alert ("Make sure you only enter numbers and no spaces, then try again.");
            }
        });
    });
</script>
<form onsubmit="return false;">
    <label>Sides</label>
    <input id="dice-sides" type="text" value="6">
    <label>Count</label>
    <input id="roll-count" type="text" value="1">
    <button id="rollDice">Roll</button>
</form>
Rolls
<div id="output"> 

</div>
于 2012-04-12T03:29:51.533 に答える