Javascript退出Loop Early

时间:2012-04-12 03:09:56

标签: javascript loops for-loop

我有一个循环用于我正在制作的硬币翻转程序。问题是它似乎很早就要退出了。看一看。

$(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循环似乎完成之前警告滚动。为什么要这样做?

2 个答案:

答案 0 :(得分:2)

您在该代码中有几个错误,但解释您所看到的行为的错误是您每次通过循环将rolls的值重置为初始字符串。

一旦你移出那条线并且你得到一个更接近的值,但是你也在计算rollsMinOne来自rolls,而不是diceRolls,如你所愿(这就是选择好的原因)名称是如此重要),意味着if语句永远不为真(因为字符串减去数字是值NaN“非数字”,它不等于任何 [甚至本身!])。

然后唯一的功能(而不是样式或设计)问题是,即使您已经使用句点添加了逗号,也会在末尾添加逗号值。

全部放在一起:

    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 + ", ");
        }

虽然正如其他答案所提到的那样,有更简单,更快捷的方法来获得相同的结果,我觉得理解为什么代码不起作用很重要。

答案 1 :(得分: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>