简体   繁体   English

Javascript如果/否则不起作用。 奥丁项目的石头,纸,剪刀

[英]Javascript if/else not working. Odin project's Rock, paper, scissors

I've been trying to solve the Rock, Paper and Scissors project but I can't figure out how to make the if/else statement. 我一直在尝试解决Rock,Paper和Scissors项目,但是我不知道如何制作if / else语句。 I did it a lot of times and finally I think I'm near to solve the issue, but the thing is, every time I run the program I get the wrong output. 我做了很多次,最后我认为我已经可以解决问题了,但事实是,每次我运行程序时,我都会得到错误的输出。

For example, I used 'Paper' and the computer used 'Rock', but the console showed that 'It's a tie', but in the code I wrote that if player chose paper and computer chose rock the message should have been 'You win. 例如,我使用“纸”,而计算机使用“ Rock”,但控制台显示“是平局”,但是在代码中,我写道,如果玩家选择纸而计算机选择了岩石,则该信息应为“您赢了” 。 Paper beats Rock'. 纸击败了摇滚。 The same happens when I choose 'Paper' and the computer chooses 'Scissors'. 当我选择“纸张”并且计算机选择“剪刀”时,也会发生同样的情况。

I used toLowerCase() method but after many changes, even when remove it and write exactly the words in the if/else this does not appear to be the problem. 我使用了toLowerCase()方法,但是在进行了许多更改之后,即使将其删除并在if / else中准确地写出单词,这也不是问题。

What do I need to correct? 我需要纠正什么? Thank you so much!!! 非常感谢!!!

This is the code: 这是代码:

 <!DOCTYPE html> <html> <head> <title>Rock Paper Scissors!</title> </head> <body> <script> //Computer's selection function computerPlay() { let selectRandom = Math.floor(Math.random() * 3); if (selectRandom === 0) { return 'Rock'; } else if (selectRandom === 1) { return 'Paper'; } else { return 'Scissors'; } } console.log('Computer chose: ' + computerPlay()); //Play round between humand and computer function playRound(playerSelection, computerSelection) { //Change to lowercase let player = playerSelection.toLowerCase(); let computer = computerSelection.toLowerCase(); //If player chooses rock if (player === 'rock') { if (computer === 'rock') { return 'It\\'sa tie. Try again' } else if (computer === 'paper') { return 'You loose. Paper beats Rock' } else { return 'You win. Rock beats scissors' } } //If player chooses paper else if (player === 'paper') { if (computer === 'paper') { return 'It\\'sa tie. Try again' } if (computer === 'scissors') { return 'You loose. Scissors beats Paper' } else { return 'You win. Paper beats Rock' } } //If player chooses scissors else { if (computer === 'scissors') { return 'It\\'sa tie. Try again' } else if (computer === 'rock') { return 'You loose. Rock beats Scissors' } else { return 'You win. Scissors beats Paper' } } } const playerSelection = 'Paper'; const computerSelection = computerPlay(); console.log(playRound(playerSelection, computerSelection)); </script> </body> </html> 

As you must have already known, every time you call computerPlay() , computer chooses a random value. 众所周知,每次调用computerPlay() ,计算机都会选择一个随机值。

When you call computerPlay() in below statement, you get a random selection. 在下面的语句中调用computerPlay()时,将获得随机选择。

console.log('Computer chose: ' + computerPlay()); // Random selection.

And when you call the function again later in the code as shown below, you get another random selection. 而且,当您稍后在代码中再次调用该函数时,如下所示,您将获得另一个随机选择。

 const computerSelection = computerPlay(); // Random selection.
 console.log(playRound(playerSelection, computerSelection));

This is the reason the output you see is not consistent with the actual result. 这就是您看到的输出与实际结果不一致的原因。

Solution: Move above console.log() statement to execute after computer selection, and log the returned value, don't call computerPlay() again. 解决方案:移动到console.log()语句上方以在选择计算机后执行,并记录返回的值,不要再次调用computerPlay() If you call it again, you'll get another random selection, not the one you are passing to playRound() function. 如果再次调用它,您将获得另一个随机选择,而不是传递给playRound()函数的选择。

const computerSelection = computerPlay();
console.log('Computer chose: ' + computerSelection);
console.log(playRound(playerSelection, computerSelection));

Working Example: 工作示例:

 <!DOCTYPE html> <html> <head> <title>Rock Paper Scissors!</title> </head> <body> <script> //Computer's selection function computerPlay() { let selectRandom = Math.floor(Math.random() * 3); if (selectRandom === 0) { return 'Rock'; } else if (selectRandom === 1) { return 'Paper'; } else { return 'Scissors'; } } //Play round between human and computer function playRound(playerSelection, computerSelection) { //Change to lowercase let player = playerSelection.toLowerCase(); let computer = computerSelection.toLowerCase(); //If player chooses rock if (player === 'rock') { if (computer === 'rock') { return 'It\\'sa tie. Try again' } else if (computer === 'paper') { return 'You loose. Paper beats Rock' } else { return 'You win. Rock beats scissors' } } //If player chooses paper else if (player === 'paper') { if (computer === 'paper') { return 'It\\'sa tie. Try again' } if (computer === 'scissors') { return 'You loose. Scissors beats Paper' } else { return 'You win. Paper beats Rock' } } //If player chooses scissors else { if (computer === 'scissors') { return 'It\\'sa tie. Try again' } else if (computer === 'rock') { return 'You lose. Rock beats Scissors' } else { return 'You win. Scissors beats Paper' } } } const playerSelection = 'Paper'; const computerSelection = computerPlay(); console.log('Computer chose: ' + computerSelection); console.log(playRound(playerSelection, computerSelection)); </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM