我是Java脚本和编码的新手。我试图找出Rock,paper,scissors,lizard,spock中的语法错误,这会弄乱我的代码。当我编译代码时,它说"语法错误:意外的令牌{"我无法弄清楚为什么以及在哪里搞砸了。谢谢你的帮助!
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice <= 0.20) {
computerChoice = "rock";
}
else if (computerChoice <= 0.40) {
computerChoice = "paper";
}
else if (computerChoice <= 0.60) {
computerChoice = "scissors";
}
else if (computerChoice <= 0.80) {
computerChoice = "lizard";
}
else {
computerChoice = "spock";
}
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 if (choice2 === "paper") {
return "paper wins";
}
else if (choice2 === "lizard") {
return "rock wins";
}
else {
return "spock wins";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}
else if (choice2 === "scissors") {
return "scissors win";
}
else if (choice2 === "lizard") {
return "lizard wins";
}
else {
return "paper wins";
}
}
else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
}
else if (choice2 === "paper") {
return "scissors win";
}
else if (choice2 === "lizard") {
return "scissors win";
}
else {
return "spock wins";
}
}
else if (choice1 === "lizard") {
if (choice2 === "rock") {
return "rock wins";
}
else if (choice2 === "scissors") {
return "scissors win";
}
else if (choice2 === "paper") {
return "lizard wins";
}
else {
return "lizard wins";
}
}
else {
if (choice2 === "rock") {
return "spock wins";
}
else if (choice2 === "paper") {
return "paper wins";
}
else if (choice2 === "scissors") {
return "spock nigga wins";
}
else {
return "lizard wins";
}
}
};
compare(userChoice, computerChoice);
答案 0 :(得分:0)
你错过了一个右括号:
else if (choice2 === "scissors"