我是新来的,刚刚开始通过 Odin 项目学习 JS。我正在进行我的第一个 JS 项目(石头、剪刀、布应用程序),但我陷入了困境......我将在下面总结我的意图,然后是我的代码。//用户输入石头、剪刀、布的值//将用户的值与计算机随机选择的值进行比较//根据用户输入和计算机生成的比较,将给出返回语句已经尝试了几乎所有的方法,但不知道如何继续前进!请帮忙。 function computerPlay() { let gameOptions = ['rock', 'paper', 'scissors']; const gameChoice = Math.floor(Math.random() * gameOptions.length); console.log(gameChoice, gameOptions[gameChoice]); } function playRound(playerSelection,computerSelection){ if(playerSelection === 'rock' && computerSelection === 'paper') { return console.log('You lose! Paper beats rock.'); }else if(playerSelection === 'paper' && computerSelection === 'scissors') { return console.log('You lose! Scissors beats paper.'); }else if(playerSelection === 'scissors' && computerSelection === 'rock') { return console.log('You lose! Rock beats scissors'); }else if(playerSelection === 'rock' && computerSelection === 'rock') { return console.log('Its a draw!'); }else if(playerSelection === 'paper' && computerSelection === 'paper') { return console.log('Its a draw!'); }else if(playerSelection === 'scissors' && computerSelection === 'scissors') { return console.log('Its a draw!'); }else if(playerSelection === 'paper' && computerSelection === 'rock') { return console.log('You win!') }else if(playerSelection === 'rock' && computerSelection === 'scissors') { return console.log('You win!') }else { return console.log('You win!') } } const playerSelection = prompt('Choose either rock, paper, or scissors', ''); const computerSelection = computerPlay(); console.log(playRound(playerSelection, computerSelection));我希望第二个函数中的条件语句能够完成此任务。仍在尝试理解函数、参数和参数。任何和所有的帮助将不胜感激!谢谢。
2 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
computerPlay()
不返回任何值。所以computerSelection
将是未定义的。
另外,将此行更改console.log(playRound(playerSelection, computerSelection));
为 justplayRound(playerSelection, computerSelection);
并return
从 中删除语句playRound()
。
此外,您的逻辑可以大大简化。例如,if (playerSelection === computerSelection) console.log("It's a draw");
添加回答
举报
0/150
提交
取消