2017-11-04 42 views
1

最初の関数には入力が含まれ、2番目の関数にはif/elseロジックがあります。私は最初に2番目の関数を呼び出していますが、if/else文は機能していないようです。Javascript:別の関数内で関数を呼び出す

var choice = function(){ 
 

 
    var userChoice = prompt("Do you choose rock, paper or scissors?"); 
 
    var computerChoice = Math.random(); 
 
    if (computerChoice < 0.34) { 
 
    \t computerChoice = "rock"; 
 
    } else if(computerChoice <= 0.67) { 
 
    \t computerChoice = "paper"; 
 
    } else { 
 
    \t computerChoice = "scissors"; 
 
    } console.log("Computer: " + computerChoice); 
 
    compare(userChoice, computerChoice); 
 
}; 
 

 
var compare = function(choice1, choice2){ 
 

 
    if(choice1 === choice2){ 
 
     return "The result is a tie!"; 
 
    } 
 
    else if(choice1 === "rock"){ 
 
     if(choice2 === "scissors"){ 
 
      return "rock wins"; 
 
     } 
 
     else{ 
 
      return "paper wins"; 
 
     }  
 
    } 
 
    else if(choice1 === "paper"){ 
 
     if(choice2 === "rock"){ 
 
      return "paper wins"; 
 
     } else{ 
 
      return "scissors wins"; 
 
     } 
 
    } 
 
    else if(choice1 === "scissors"){ 
 
     if(choice2 === "rock"){ 
 
      return "rock wins"; 
 
     } else{ 
 
      return "scissors wins"; 
 
     } 
 
    } 
 
    else{ 
 
     return "invalid input"; 
 
    } 
 
}; 
 

 
choice();

+0

あなたの 'compare()'関数は、あなたが決して使っていない文字列を返します。ユーザーに結果を見たい場合は、 'alert(compare(userChoice、computerChoice));'を試してみてください。 – nnnnnn

答えて

0

それはうまく動作します。

compare(userChoice, computerChoice) 

戻り適切な結果。

しかし、あなたはそれを表示しませんでした。 alert()などで表示してください:

alert(compare(userChoice, computerChoice)); 
関連する問題