2017-09-26 21 views
0

私はこのプログラムを実行するたびに、プロンプトに岩や紙を入れても、「はさみ対x」という警告がポップアップします。もし私が紙の中に入れたら、私は岩の中に置くか「紙とx」を入れれば「ロックとx」と言わなければならないが、何らかの理由で常に私が入れた値が同じ値コンピュータが生成されます。私のロックペーパーはさみプログラムが機能しないのはなぜですか?

function setup() { 

    alert("Welcome to Rock Paper Scissors!"); 
} 

function draw() { 

var opponentChoice = Math.random(); //Setup for game 
var yourChoice = prompt("Rock, paper or scissors?"); 
if (opponentChoice <0.34){ 
    opponentChoice = "rock"; 
}else if(opponentChoice <=0.67){ 
    opponentChoice = "paper"; 
} 
else{ 
    opponentChoice = "scissors"; 
} //End of setup for game 

function compare (firstOption,secondOption){ //Game logic 
    if(firstOption===secondOption){ //Tie if both are the same 
     alert(firstOption + " vs. " + secondOption); 
     alert("It was a tie!"); 
    } 
    else if(firstOption==="rock"){ //If user types rock 
     alert("Rock vs. " + secondOption); 
     if(secondOption==="scissors"){ //If computer generates scissors 
      alert("Rock wins"); 
     } 
     else { //If computer generates paper 
      alert("Paper wins"); 
     } 
    } 
    else if(firstOption==="scissors"){ //If user types scissors 
     alert("Scissors vs. " + secondOption); 
     if(secondOption==="rock"){ //If computer generates rock 
      alert("Rock wins!"); 
     } 
     else { //If computer generates paper 
      alert("Scissors wins"); 
     } 
    } 
    else if(firstOption==="paper"){ //If user types paper 
     alert("Paper vs. " + secondOption); 
     if(secondOption==="rock"){ //If computer generates rock 
      alert("Paper wins"); 
     } 
     else { //If computer generates scissors 
      alert("Scissors wins"); 
     } 
    } 
    else{ 
     alert("Please choose one of the options in lower case"); 
    } 
}; 
compare(yourChoice,opponentChoice); //Function is called here 

} 
+0

はあなたがエラーを発見した例との完全なコードを私に提供することができます作品のように見えますか? – zhuravlyov

答えて

0

"use strict"; 
 

 
function draw() { 
 

 
    let opponentChoice = Math.random(); //Setup for game 
 
    let yourChoice = prompt("Rock, paper or scissors?"); 
 
    if (opponentChoice <0.34){ 
 
     opponentChoice = "rock"; 
 
    }else if(opponentChoice <=0.67){ 
 
     opponentChoice = "paper"; 
 
    } 
 
    else{ 
 
     opponentChoice = "scissors"; 
 
    } //End of setup for game 
 

 
    function compare (firstOption,secondOption){ //Game logic 
 
     if(firstOption===secondOption){ //Tie if both are the same 
 
      alert(firstOption + " vs. " + secondOption); 
 
      alert("It was a tie!"); 
 
     } 
 
     else if(firstOption==="rock"){ //If user types rock 
 
      alert("Rock vs. " + secondOption); 
 
      if(secondOption==="scissors"){ //If computer generates scissors 
 
       alert("Rock wins"); 
 
      } 
 
      else { //If computer generates paper 
 
       alert("Paper wins"); 
 
      } 
 
     } 
 
     else if(firstOption==="scissors"){ //If user types scissors 
 
      alert("Scissors vs. " + secondOption); 
 
      if(secondOption==="rock"){ //If computer generates rock 
 
       alert("Rock wins!"); 
 
      } 
 
      else { //If computer generates paper 
 
       alert("Scissors wins"); 
 
      } 
 
     } 
 
     else if(firstOption==="paper"){ //If user types paper 
 
      alert("Paper vs. " + secondOption); 
 
      if(secondOption==="rock"){ //If computer generates rock 
 
       alert("Paper wins"); 
 
      } 
 
      else { //If computer generates scissors 
 
       alert("Scissors wins"); 
 
      } 
 
     } 
 
     else{ 
 
      alert("Please choose one of the options in lower case"); 
 
     } 
 
    } 
 

 
    compare(yourChoice,opponentChoice); //Function is called here 
 

 
} 
 

 
draw();

それは罰金

関連する問題