2015-06-18 55 views
0

我目前在學習JavaScript,並且一直有這個錯誤!SyntaxError:意外的代幣如果

這是我的腳本:

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"; 
     } 

回答

0

應該是:

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"; 
} 

或者更簡潔:

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" 
    } 
} 
+0

非常感謝wZVanG!有用。 –

+0

@BrandonMagro請檢查[Tour](http://stackoverflow.com/tour)。如果你的問題解決了,你可以標記正確的答案。 –