2013-03-08 55 views
0

我为我的网页设计类&分配了一个基本代码,我们需要修改它来做更多事情。只要我添加一个if,else if语句,javascript就停止工作。我无法弄清楚我要去哪里错了。如果有人能引导我走向正确的方向,我会非常感激。 这里的原代码(用音符一个是我们需要做的):添加if-else语句会导致JavaScript停止工作

<!DOCTYPE html> 
<!-- Finish the logic of the Rock Paper Scissors game below. 
    See the comments for instructions. --> 
<html> 
    <head> 
    <title>RPS</title> 
    <script> 
    function rps() { 
     userChoice = prompt("Rock, paper or scissors?"); 
     userPick.innerHTML = userChoice; 
     computerChoice = Math.random(); 
     if (computerChoice < 0.34) { 
      computerChoice = "rock"; 
     } else if (computerChoice < 0.67) { 
      computerChoice = "paper"; 
     } else { 
      computerChoice = "scissors"; 
     } 
     computerPick.innerHTML = computerChoice; 
     result = compare(userChoice, computerChoice); 
     if (result == 1) { 
      // set the outcome entity to say that "Player Wins!" 
      // also increment the user's score. 
     } else if (result == -1) { 
      // set the outcome entity to say that "Computer Wins!" 
      // also increment the computer's score. 
     } else { 
      // set the outcome entity to say "Tie" 
     } 
    } 

    function compare(choice1, choice2) { 
     // return 0 if choices are equal, 
     // 1 if choice1 wins, 
     // -1 if choice2 wins 
    } 
    </script> 
    </head> 
    <body> 
     <h2>RockPaperScissors</h2> 
     <p id="outcome">-</p> <b> computer picks: </b> 
     <p id="computerPick">-</p> <b> player picks: </b> 
     <p id="userPick">-</p> 
     <!-- create p blocks to store the computer's score (number of wins) 
     and the user's score. These will have to be modified by the rps function 
     --> 
     <button type="button" onclick="rps()">Go</button> 
    </body> 
</html> 

这是我改变了功能比较方法:

function compare(choice1, choice2) { 
    // return 0 if choices are equal, 
    // 1 if choice1 wins, 
    // -1 if choice2 wins 

    if (choice1 == "paper" && choice2 == "rock" || 
     choice1 == "rock" && choice2 == "scissors" || 
     choice1 == "scissors" && choice2 == "paper") { 
     return == 1; 
    } 
    else if (choice1 == "paper" && choice2 == "scissors" || 
      choice1 == "rock" && choice2 == "paper" || 
      choice1 == "scissors" && choice2 == "rock") { 
     return == -1; 
    } 
    else { 
     return == 0; 
    } 
} 
+0

@Samuel刘氏:请阅读作业标签维基。不要将它添加到新问题中。 – Mat 2013-03-08 05:25:15

回答

-1
  return 1 

  return == 1 

返回是一个关键字而不是变量。 =是赋值运算符,它将运算符右侧的值放入左侧的变量中。 ==是相等运算符。

+1

'=='是平等运算符。 – Blender 2013-03-08 03:05:10

+0

我认为OP会让Pascal感到宾至如归。 – hugomg 2013-03-08 03:05:55

1

从您的退货中删除==。他们应该只是...

return 1;

==用于比较。

+0

谢谢,这样一个简单的错误,但我只是看不到它。 – 2013-03-08 03:19:32

0

更改compare功能

function compare(choice1, choice2) { 
      // return 0 if choices are equal, 
      // 1 if choice1 wins, 
      // -1 if choice2 wins 

      if (choice1=="paper" && choice2=="rock"||choice1=="rock" 
&& choice2=="scissors"||choice1=="scissors" && choice2=="paper"){ 
       return 1;} 
      else if (choice1=="paper" && 
choice2=="scissors"||choice1=="rock" && choice2=="paper"||choice1=="scissors" && 
choice2=="rock"){ 
       return -1;} 
      else { 
       return 0;} 
     } 
+0

谢谢,这样一个简单的错误,但它让我发疯。 – 2013-03-08 03:20:19