让我们成交JavaScript代码不起作用

时间:2016-12-25 04:30:08

标签: javascript

对于deal()函数,每次为curtain1输入1时,它都能正常工作,但每次为curtain2输入2时,它只会说“你想再玩一次,是或否?”但是,它不在代码的第2部分。

<script type="text/javascript">
    alert("Welcome to the arcade filled with MANY games");
    var userName = prompt("What is your name").toUpperCase()
    alert("Welcome " + userName)
    var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ")
    while (select != 1 && select != 2)
        var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ")
    if (select == 1)
        start()
    else if (select == 2)
        instructions()


    function menu() {
        var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ")
        while (select != 1 && select != 2)
            var select = prompt("1.Start Game \n \n 2. Instructions \n \n select: ")
        if (select == 1)
            start()
        else if (select == 2)
            instructions()
    }

    function start() {
        var option = prompt("1. Black Jack \n \n 2, MAKE A DEAL \n \n TO end type QUIT \n \n Select: ").toUpperCase()
        while (option != 1 && option != 2)
            var option = prompt("1. Black Jack \n \n 2, MAKE A DEAL \n \n TO end type QUIT \n \n Select: ").toUpperCase()
        if (option == 1)
            blackJack()
        else if (option == 2)
            deal(userName)
        else if (option == "QUIT")
            menu()

    }

    function blackJack() {
        alert("Welcome to BlackJack")
        var dealerCard = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var dealer2 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardsTwo = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardHit = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardHit2 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardHit3 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardHit4 = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardsThree = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardsFour = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var cardsFive = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "11"]
        var ranCards = Math.floor(Math.random() * cards.length);
        var ranTwo = Math.floor(Math.random() * cardsTwo.length);
        var ranThree = Math.floor(Math.random() * cardsThree.length);
        var ranFour = Math.floor(Math.random() * cardsFour.length);
        var ranFive = Math.floor(Math.random() * cardsFive.length);
        var ranHit = Math.floor(Math.random() * cardHit.length);
        var ranHit2 = Math.floor(Math.random() * cardHit2.length);
        var ranHit3 = Math.floor(Math.random() * cardHit3.length);
        var ranHit4 = Math.floor(Math.random() * cardHit4.length);
        var dealerRan = Math.floor(Math.random() * dealerCard.length)
        var dealerRan2 = Math.floor(Math.random() * dealer2.length)

        var add = (ranCards + ranTwo)
        if (add > 21)
            var gameOver = prompt("GAME OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
        if (gameOver == "YES")
            blackJack()
        else if (gameOver == "NO")
            menu()
        else if (add < 21)
            firstPart(add)


        function firstPart(add) {
            var userCards = prompt(ranCards + " and " + ranTwo + " do you want to HIT OR STAY?").toUpperCase()
            while (userCards != "HIT" && userCards != "STAY")
                var userCards = prompt(ranCards + " and " + ranTwo + " do you want to HIT OR STAY?").toUpperCase()
            if (userCards == "HIT")
                var add2 = (add + ranHit)
            if (add2 > 21)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()
            else if (add2 < 21)
                secondPart(add2)
            else if (userCards == "STAY")
                var dealer = (dealerRan + dealerRan2)
            alert("You finished with " + add + " the dealer finsihed with " + dealer)
            if (add > dealer)
                var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOverWin == "YES")
                blackJack()
            else if (gameOverWin == "NO")
                menu()
            else if (add < dealer)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()

        }

        function secondPart(add2) {
            var userCards2 = prompt("You have " + add2 + " do you want to HIT OR STAY").toUpperCase()
            while (userCards2 != "HIT" && userCards2 != "STAY")
                var userCards2 = prompt("You have " + add2 + " do you want to HIT OR STAY").toUpperCase()
            if (userCards2 == "HIT")
                var add3 = (add2 + ranHit2)
            if (add3 > 21)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()
            else if (add3 < 21)
                thirdPart(add3)

            else if (userCards2 == "STAY")
                var dealer = (dealerRan + dealerRan2)
            alert("You finished with " + add2 + " the dealer finsihed with " + dealer)
            if (add > dealer)
                var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOverWin == "YES")
                blackJack()
            else if (gameOverWin == "NO")
                menu()
            else if (add < dealer)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()

        }

        function thirdPart(add3) {
            var userCards3 = prompt("You have " + add3 + " do you want to HIT OR STAY").toUpperCase()
            while (userCards3 != "HIT" && userCards3 != "STAY")
                var userCards3 = prompt("You have " + add3 + " do you want to HIT OR STAY").toUpperCase()
            if (userCards3 == "HIT")
                var add4 = (add3 + ranHit3)
            if (add4 > 21)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()
            else if (add4 < 21)
                fourthPart(add4)

            else if (userCards3 == "STAY")
                var dealer = (dealerRan + dealerRan2)
            alert("You finished with " + add3 + " the dealer finsihed with " + dealer)
            if (add > dealer)
                var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOverWin == "YES")
                blackJack()
            else if (gameOverWin == "NO")
                menu()
            else if (add < dealer)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()
        }

        function fourthPart(add4) {
            var userCards4 = prompt("You have " + add4 + " do you wan to HIT OR STAY").toUpperCase()
            while (userCards4 != "HIT" && userCards4 != "STAY")
                if (userCards4 == "HIT")
                    var add5 = (add4 + ranHit4)
            if (add5 > 21)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()
            else if (add5 < 21)
                fifthPart(add5)
            else if (userCards4 == "STAY")
                var dealer = (dealerRan + dealerRan2)
            alert("You finished with " + add4 + " the dealer finsihed with " + dealer)
            if (add > dealer)
                var gameOverWin = prompt("YOU WINNN!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOverWin == "YES")
                blackJack()
            else if (gameOverWin == "NO")
                menu()
            else if (add < dealer)
                var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()

        }

        function fifthPart(add5) {
            alert("YOU BEAT THE GAME!!!! YOU FINISHED WITH" + add5)
            var gameOver = prompt("Game OVERRRRRR!!! Do you want to play again? YES OR NO").toUpperCase()
            if (gameOver == "YES")
                blackJack()
            else if (gameOver == "NO")
                menu()
        }

    }

    function deal(userName) {
        curtain1 = Array("MOTORCYLE", "ZONK", "ZONK", "COOKING TOOLS", "LA FITNESS 1 YEAR PASS", "COMPAC LAPTOP")
        curtain2 = Array("ZONK", "ZONK", "BMW", "YEEZY'S", "IPHONE 7PLUS")
        curtain3 = Array("FERRARI 458", "ZONK", "ZONK", "ZONK", "KFC LIFETIME MEMBERSHIP")
        ran1 = Math.floor(Math.random() * curtain1.length)
        ran2 = Math.floor(Math.random() * curtain2.length)
        ran3 = Math.floor(Math.random() * curtain3.length)
        alert("COME ON DOWN " + userName + " MAKE A DEAL WITH US! ")
        var dealSelect = prompt("1. Curtain \n \n 2. BOX \n \n 3. Envolope \n \n Select: ")
        if (dealSelect == 1)
            var curtainSelect = prompt("Which curtain would you like? Curtain 1? Curtain 2? or Curtain 3?")
        var won = curtain1[ran1]
        if (curtainSelect == 1)
            if (won == "ZONK")
                alert("DUN DUN DUNHHH YOU GOT ZONKED")
            else if (won != "ZONK")
            alert("YOU JUST WON " + won + " CONGRATULATIONS")
        var dealOver = prompt("DO YOU WANT TO PLAY AGAIN? YES OR NO").toUpperCase()
        if (dealOver == "YES")
            deal(userName)
        else if (dealOver == "NO")
            menu()
        else if (curtainSelect == 2)
            var wonOne = curtain2[ran2]
        if (wonOne == "ZONK")
            alert("DUN DUN DUNHHH YOU GOT ZONKED")
        else if (wonOne != "ZONK")
            alert("YOU JUST WON " + wonOne + " CONGRATULATIONS")




    }



    </script>

1 个答案:

答案 0 :(得分:0)

让感觉发光 将大括号{ ... }放在多行if语句中 如果您没有放置它们,您的代码将只执行if

之后的第一条指令
else if (won != "ZONK")
{
     alert("YOU JUST WON " + won + " CONGRATULATIONS")
     // the rest of your code if won != "Zonk"

因为在这种情况下,无论值var dealOver = prompt("DO YOU WANT TO PLAY AGAIN? YES OR NO").toUpperCase()是什么,都会执行curtainSelect,因为此说明不是if语句的一部分:if (curtainSelect == 1)