我正在使用codecademy学习js,它告诉我我已经通过了课程,但是它对我来说看起来并不成功,我想了解我出错的地方帮助我学习,因为它没有提供任何帮助因为它说我已经成功完成了课程。所以任何帮助将不胜感激:)
我做了一个非常基本的摇滚,纸张,剪刀游戏。
我无法宣布哪个获胜,摇滚,纸张或剪刀。 比较变量不会输出'任何东西。
它应该说“摇滚胜利”'或者'剪刀赢了'或者'结果是平局!'等等,但它所做的只是说“用户选择Rock Computer挑选剪刀”
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);
答案 0 :(得分:2)
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(不包括),你然后可以缩放到您想要的范围。该实现选择初始种子到随机数生成算法;它不能被用户选择或重置。
答案 1 :(得分:1)
我正在用错误的案例'Rock'而不是'rock'来回答提示。如果使用正确的案例,代码就可以正常工作。