我不知道錯誤在哪裏了。它首先說我有一個意外的標識符,然後我有一個無與倫比的},現在我有一個非法的返回聲明。我只需要一些幫助。謝謝。我不知道錯誤在哪裏
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Computer: " + 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";
}
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
}
你錯過了一個大括號 – geeves
它是否可以將computerChoice設置爲數字然後作爲一個字符串呢?只是好奇,我可能會有2個變量,每個類型一個。 – Forklift
31行有一個「else if」跟在「else」之後,那個「Else If」永遠不會到達。 在JavaScript中,一個變量可以是一個數字,然後是一個字符串。 – geeves