开发者

Am I formatting my switch statements wrong?

开发者 https://www.devze.com 2023-02-24 03:38 出处:网络
I have been working on a \"Rock Paper Scissors\" game to see if I understand the basics of JavaScript. It all works well until I get to the if statement from lines 30 to 71 in the fight(): I have adju

I have been working on a "Rock Paper Scissors" game to see if I understand the basics of JavaScript. It all works well until I get to the if statement from lines 30 to 71 in the fight(): I have adjusted the reply variable to indicate how it may be "if'ing" through. It seems to be returning the default value of every sub "if" in the overall "if". Am I using the wrong variable for the secondary condition maybe?

radioGroup.html

    <script type = "text/javascript">
      //<![CDATA[
      // from radioGroup.html
      function fight(){
        var randomChoice = Math.random();
        var threeChoice = Math.floor (randomChoice * 3);
        var weapon = document.getElementsByName("weapon");

        for (i = 0; i < weapon.length; i++){
          currentWeapon = weapon[i];

          if (currentWeapon.checked){
            var selectedWeapon = currentWeapon.value;
          } // end if

        } // end for

        var cpuChoice = new Array("Paper", "Rock", "Scissors")
        var reply = "xxx"
        if (threeChoice === 0) {
            if (weapon === "Paper") {
                reply = "11";
                }
            else if (weapon === "Rock") {
                    reply = "12";
                }
            else if (weapon === "Scissors") {
                    reply = "13";
                    }
            else {
                    reply = "what1";
                    }
        }else if (threeChoice === 1) {
                if (weapon === "Paper") {
                reply = "21";
                }
            else if (weapon === "Rock") {
                    reply = "22";
                }
            else if (weapon === "Scissors") {
                    reply = "23";
                    }
            else {
                    reply = "what2";
                    }
        }else if (threeChoice === 2) {
                if (weapon === "Paper") {
                reply = "31";
                }
            else if (weapon === "Rock") {
                    reply = "32";
                }
            else if (weapon === "Scissors") {
                    reply = "33";
                    }
            else {
                    reply = "what3";
                    }
        }else {
            reply = "hay now?";
            }
        var output = document.getElementById("output");
        var response = "<h2>You have chosen ";
        response += selectedWeapon + "<h2>The CPU has chosen ";
        "<\/h2> \n";
        response += cpuChoice[threeChoice] + "<\/h2> \n";
        response += reply + "<\/h2> \n";
        output.innerHTML = response;
     } // end function

      //]]>
    </script>
  </head>

  <body>
    <h1>Choose!</h1>
    <form action = "">
      <fieldset>
        <input type = "radio"
               name = "weapon"
               id = "radPaper"
               value = "Paper"
               che开发者_JAVA百科cked = "checked" />
        <label for = "radPaper">Paper</label>

        <input type = "radio"
               name = "weapon"
               id = "radRock"
               value = "Rock" />
        <label for = "radRock">Rock</label>

        <input type = "radio"
               name = "weapon"
               id = "radScissors"
               value = "Scissors" />
        <label for = "radScissors">Scissors</label>
        <button type = "button"
                onclick = "fight()">
          fight the dragon
        </button>
      </fieldset>
    </form>
    <div id = "output">

    </div>
  </body>
</html>


You are correct about using the switch condition incorrectly, the "case" is what the expression in your switch evaluates to.

you should be using if statements, but you can use a switch statement like this

switch (weapon === "paper") {
case true:
   //do something
   break;
case false:
   // do something else
default:
   // oh no! it wasn't true of false
}


case is for testing simple values, usually numeric. For your case, I'd suggest assigning numbers to rock, paper, and scissors, such that the ordering of those numbers makes it clear who won. For example, rock=0, paper=1, scissors=2. Then, (3 + (a - b)) % 3 will tell you who won: 0 is a tie, 1 indicates that a won, and 2 indicates that b won. Then you can do a simple three-case switch over the result of that operation.

0

精彩评论

暂无评论...
验证码 换一张
取 消