这个JavaScript石头剪子布游戏的错误是什么?



我知道有很多这样的问题,但我想知道为什么这不起作用。我刚开始学习js,想做一个石头剪刀布小游戏,但我遇到了一个问题。但是我好像真的找不到。

function getUserChoice(userNumber) {
userNumber = prompt('Rock, Paper, Scissors?')
}
getUserChoice()
const getComputerChoice = () => {
const randomNumber = (Math.floor(Math.random() * 3))
if (randomNumber === 0) {
return 'Rock';
} else if (randomNumber === 1) {
return 'Paper';
} else if (randomNumber === 2) {
return 'Scissor';
}
}
function game() {
if (getUserChoice === getComputerChoice) {
return 'Tie';
} else if (getUserChoice === 'Rock' && getComputerChoice === 'Paper') {
return 'Computer Wins';
} else if (getUserChoice === 'Paper' && getComputerChoice === 'Scissors') {
return 'Computer Wins';
} else if (getUserChoice === 'Scissors' && getComputerChoice === 'Rock') {
return 'Computer Wins';
} else if (getComputerChoice === 'Scissors' && getUserChoice === 'Rock') {
return 'You win';
} else if (getComputerChoice === 'Scissors' && getUserChoice === 'Rock') {
return 'You win';
} else if (getComputerChoice === 'Scissors' && getUserChoice === 'Rock') {
return 'You win';
} else {
return null;
}
}
document.write(game())
/* Basic CSS comment */
body {
background: rgb(182, 153, 160);
width: 960px;
margin: 0;
font-size: 80px;
color: rgb(107, 89, 93);
}
<html>
<head>
<title>Rock, Paper, Scissors</title>
</head>
<body>
</body>
</html>

  • 在getUserChoice函数中,您不需要userNumber参数,您可能希望返回prompt函数的输出。
  • 在游戏函数中,getUserChoice和getComputerChoice引用函数-如果你想引用它们的输出,说var userChoice = getUserChoice();和类似的getComputerChoice,然后在你的大if语句中使用这些变量。

最新更新