gpt4 book ai didi

javascript - 我不知道错误出在哪里了

转载 作者:太空宇宙 更新时间:2023-11-04 15:59:46 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 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";
}
}

最佳答案

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

正确缩进的代码可以帮助找到丢失的大括号。当某个东西缩进 4 次时,就该看看是否可以重构了。

例如:

第一个 if 有返回值。之后没有理由有“否则如果”。这将消除一个缩进。 ("choice2 === "rock") 也是如此。相信当写入返回时,它实际上会返回。

关于javascript - 我不知道错误出在哪里了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42373534/

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