gpt4 book ai didi

javascript - Codecademy 上的石头剪刀布游戏

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

我尝试合并网站(Codecademy)上给出的附加建议,即。存在平局的功能以及再次接受输入的规定。这是包含错误的代码段:

var userChoice, computerChoice;

var choicesDetermination = function(){
userChoice = prompt("Do you choose rock, paper or scissors?");
if(userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors"){
userChoice = prompt("Invalid input. Please try again.\n Do you choose rock, paper or scissors?");
}
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("User: " + userChoice);
console.log("Computer: " + computerChoice);
}

var compare = function(choice1, choice2){
if(choice1 === choice2){
console.log("The result is a tie! Let's try one more time.");
choicesDetermination();
compare(userChoice, computerChoice);
}
else if(choice1 === "rock"){
if(choice2 === "scissors"){
return "rock wins";
}
else{
return "paper wins";
}
}
else if(choice1 === "paper"){
if(choice2 === "rock"){
return "paper wins";
}
else{
return "scissors wins";
}
}
else{
if(choice1 === "scissors"){
if(choice2 === "paper"){
return "scissors wins";
}
else{
return "rocks wins";
}
}
}
}
choicesDetermination();
compare(userChoice, computerChoice);

这里,choicesDetermination() 是我获取输入并将其存储在 userChoice、computerChoice(均为全局变量)中的函数。我不知道为什么,但当我再次要求输入时,代码似乎运行良好;变量已正确更改。但是函数compare()运行不正确; return 语句不会打印到屏幕上。

最佳答案

我在 Code Academy 中一直发现输出字符串必须非常完美,所以首先检查一下!

我认为您的代码的问题在于您实际上应该返回“结果是平局!让我们再试一次。” console.log.

请在下面找到我完整的传递代码:

var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice != "rock" && "scissors" && "paper") {
alert("Please enter 'rock', 'scissors', or 'paper' as shown.");
userChoice = prompt("Type carefully, please: 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";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
else {
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
};

compare(userChoice, computerChoice);

关于javascript - Codecademy 上的石头剪刀布游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450108/

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