gpt4 book ai didi

javascript - 意外的 token else Codecademy

转载 作者:行者123 更新时间:2023-12-03 09:19:03 25 4
gpt4 key购买 nike

我遇到了一个语法错误的问题(意外的其他情况)下面是我的代码。我认为这将是一个“大括号”或分号问题

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 swins";
} 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 部分添加了一个缺少的右括号 else if (choice1 === "paper")

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";

} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock swins";
} 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"
}
}
}
};

如果还有任何错误,请告诉我。

关于javascript - 意外的 token else Codecademy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31889664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com