【发布时间】:2017-12-29 21:35:45
【问题描述】:
我创建了一个基本的石头剪刀布游戏,我无法在函数内返回结果。
我什么都试过了
链接控制台记录比较并将其放入变量中..
console.log(compare) 由于某种原因返回 undefined。
请帮忙
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
console.log("Your choice: " + userChoice);
console.log(computerChoice + " vs " + userChoice);
var compare = function(userChoice, computerChoice){
if (userChoice === computerChoice){
return "The result is a tie!";
} else if (userChoice ==="rock"){
if(computerChoice ==="scissors"){
return "rock wins!";
}
else {
return "paper wins!";
}
} else if (userChoice === "paper") {
if(computerChoice === "rock") {
return "paper wins!";
}
else {
return "scissors wins!";
}
} else if (userChoice === "scissors") {
if(computerChoice === "rock") {
return "rock wins!";
}
else {
return "scissors wins!";
}
}
}
【问题讨论】:
-
return = "The result is a tie!";?为什么是等号? -
哦,这是一个诚实的错字,但删除它后,我仍然遇到同样的问题
-
好吧,我测试了 sn-p,它工作正常。我认为你相信它不会返回任何东西,因为你没有对返回的字符串做任何事情
-
你可以比较一个函数然后
alert(compare(userChoice,computerChoice))
标签: javascript function