javascript
初学者在这里。我搜索了很长时间的解决方案,但找不到解决方案。如果可以的话,请给出答案。谢谢。
computerSelection函数返回一个值。我想要一个字符串值。当我添加调试器时,它显示computerSelection = ƒ ()
。因此,即使(computerSelection === "paper")
也不会警告“比赛平局”。
function gamePlay () {
let gameSelections = ["rock","paper","scissor"];
let computerSelection = function () {
function getRandomSelection() {
return gameSelections[Math.floor(Math.random() * 3)];
}
return getRandomSelection();
}
console.log(computerSelection());
// when I added debugger; and ran the code in developer options it shows --computerSelection = ƒ ()--. So even if (computerSelection === "paper") it doesn't alert "match draw".
if (computerSelection === "paper") {
alert("match draw");
}
}
gamePlay();
我找到了一种可行的方法。但是,我仍然想了解为什么以前不起作用以及现在为什么起作用。
function gamePlay () {
let gameSelections = ["rock","paper","scissor"];
let computerSelection = function () {
function getRandomSelection() {
return gameSelections[Math.floor(Math.random() * 3) ] ;
}
return getRandomSelection();
}
let computerSelectionStringValue = computerSelection();
console.log(computerSelectionStringValue);
// this time when added debugger in developer options it shows --computerSelectionStringValue = "paper"--
if (computerSelectionStringValue === "paper") {
alert("match draw");
}
}
gamePlay();
答案 0 :(得分:0)
您对电脑的选择感到困惑。您期望它既是函数又是函数的结果。
这样想您的结果:
let result = computerSelection();
或:
if (computerSelection() == 'rock') { ... }