javascript - Am I formatting my switch statements wrong? -


i have been working on "rock paper scissors" game see if understand basics of javascript. works until if statement lines 30 71 in fight(): have adjusted reply variable indicate how may "if'ing" through. seems returning default value of every sub "if" in overall "if". using wrong variable secondary condition maybe?

radiogroup.html

    <script type = "text/javascript">       //<![cdata[       // radiogroup.html       function fight(){         var randomchoice = math.random();         var threechoice = math.floor (randomchoice * 3);         var weapon = document.getelementsbyname("weapon");          (i = 0; < weapon.length; i++){           currentweapon = weapon[i];            if (currentweapon.checked){             var selectedweapon = currentweapon.value;           } // end if          } // end          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"                checked = "checked" />         <label = "radpaper">paper</label>          <input type = "radio"                name = "weapon"                id = "radrock"                value = "rock" />         <label = "radrock">rock</label>          <input type = "radio"                name = "weapon"                id = "radscissors"                value = "scissors" />         <label = "radscissors">scissors</label>         <button type = "button"                 onclick = "fight()">           fight dragon         </button>       </fieldset>     </form>     <div id = "output">      </div>   </body> </html> 

you correct using switch condition incorrectly, "case" expression in switch evaluates to.

you should using if statements, can use switch statement this

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

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -