我正在为“奥丁项目”创建一个石头剪刀布游戏。指令说明通过使用外部函数在一轮函数中实现计算机选择随机化来创建一轮游戏。
为了练习,我试图将一个函数分配给变量名“computerChoice”。当我这样做时,它的结果是未定义的。如果我只是使用函数调用“computerPlay()”将我的参数放入 playRound(),它就可以工作。如果我为函数“computerChoice”使用分配的变量,它就不起作用。
我在网上搜索了这个,据说你可以在 Javascript 中做到这一点。我在这里做错了什么?
let choices = ['rock', 'paper', 'scissors'];
const rdm = Math.floor(Math.random() * 3);
const computer = choices[rdm];
// let playerSelection = playerSelection.toLowerCase();
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return 'It is a tie!!!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
return 'PAPER BEATS ROCK! computer wins!'
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
return 'ROCK BEATS SCISSORS! player wins!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
return 'SCISSORS BEATS PAPER! computer wins!'
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
return 'PAPER BEATS ROCK! player wins!';
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
return 'ROCK BEATS SCISSORS! computer wins!'
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
return 'SCISSORS BEATS PAPER! player wins!';
}
}
// function computerPlay() {
// const computer = choices[rdm];
// return computer;
// }
// console.log(playRound('rock', computerPlay())); // This works!
let computerChoice = function computerPlay() {
const computer = choices[rdm];
return computer;
}
console.log(playRound('rock', computerChoice)); // This does not Work!
慕尼黑的夜晚无繁华
相关分类