gpt4 book ai didi

javascript - Codecademy 帮助,变量不起作用

转载 作者:行者123 更新时间:2023-11-28 18:44:52 25 4
gpt4 key购买 nike

我正在使用codecademy学习js,它告诉我我已经通过了类(class),但是它对我来说看起来并不成功,我想了解我哪里出错了以帮助我学习,因为它没有提供任何帮助,因为它说我已经成功完成了类(class)。因此,任何帮助将不胜感激:)

我制作了一个非常基本的石头剪刀布游戏。

我无法让它宣布石头、剪刀、布谁赢了。 compare 变量不会“输出”任何内容。

应该说“石头赢了”或“剪刀赢了”或“结果是平局!”等等,但它所做的只是说“用户选择摇滚电脑选择剪刀”

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("User picks" + " " + userChoice + " " + "Computer picks" + " " + computerChoice);

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
console.log("The result is a tie!");
}
else if(choice1 === "rock") {
if(choice2 === "scissors") {
console.log("rock wins")
}
else {
console.log("paper wins")
}
}
else if(choice1 === "paper") {
if(choice2 === "rock") {
console.log("paper wins")
}
else {
console.log("scissors wins")
}
}
else if(choice1 === "scissors") {
if(choice2 === "paper") {
console.log("scissors wins")
}
else {
console.log("rock wins")
}
}
};
compare(userChoice, computerChoice);

最佳答案

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";
}
} 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";
}
}
};
compare(userChoice,computerChoice);

PS:Math.random() 函数返回 [0, 1) 范围内的浮点伪随机数,即从 0(含)到但不包括 1(不包括),您可以在然后可以缩放到您想要的范围。该实现选择随机数生成算法的初始种子;用户无法选择或重置它。

关于javascript - Codecademy 帮助,变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35539766/

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