对于下面的代码,每次调用alert函数时我都会得到"Scissors",我不确定我的错误在哪里。
var computerChoice = Math.random();
var newChoice = function (computerChoice) {
if (computerChoice <= 0.34) {
var newChoice = "rock";
return newChoice;
} else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
var newChoice = "paper";
return newChoice;
} else {
var newChoice = "scissors";
return newChoice;
}
}
var newerChoice = newChoice();
alert(newerChoice);
newChoice
函数时,您没有传递参数,因此函数的参数computerChoice
是未定义的。undefined <= 0.34
为false
,其他比较也是如此。 - Felix Kling