无法得到数字合作

时间:2016-01-27 02:10:49

标签: javascript function numbers

我正在制作一个基本的骰子游戏,目标是让这个人掷骰子,如果他们得到7,他们将4美元加到他们的赌注,其他明智的他们输掉1美元。

出于某种原因,不是将4美元加到已经确定的金额上,而是将物理上添加4英寸,所以如果这个人有5美元并且滚动7而不是添加4到5来制作9,那么它只需添加4对54,任何建议?

这里是my fiddle

  <script>


        function rollDice(){
        var userInput = document.getElementById("bet").value;
        document.getElementById("bet").value = userInput; 
        var d1 = Math.floor(Math.random() * 6) + 1;
        var d2 = Math.floor(Math.random() * 6) + 1;
        var diceTotal = d1 + d2;
        do {
            if(diceTotal === 7) {
                alert("your rolled a " +d1+ " and a " +d2+ " for a total of                      " +diceTotal +"! You win $4");
                document.getElementById("bet").value = userInput + 4;
                 break; 
            } else {
                alert("your rolled a " +d1+ " and a " +d2+ " for a total of " +diceTotal +"! You lose $1");
                document.getElementById("bet").value = userInput - 1;
                break;  
            }
        } while (userInput >= 0);
        }
    </script>
</head>
<body>          
    <div class="container-fluid">
        <div class="page-head"> 
            <h1 align="center">Lucky Sevens</h1>
        </div>
    </div>  
    <div class="container" align="center"> 
        <table style="border: 1px solid black"> 
            <tr>
                <th><h3 align="center">Lucky Sevens</h3></th>
            </tr>
            <tr>
                <td>
                    <form>
                        Starting Bet: 
                        <input id="bet" type="text"/>
                    </form>
                </td>   
            </tr>
            <tr>
                <td align="center">
                    <button onclick="rollDice()">Play</button>
                </td>
            </tr>
        </table>
    </div> 

0 个答案:

没有答案