0

I have been stuck on this for so long, basically its a human vs computer, I type in say Rock and it gives me you chose rock you win, even when i chose the others it still returns that, can someone help me to figure out why it keeps doing this?

 let person = prompt ("Rock, Paper, Scissors");

 // Computer makes a choice
 function computerPlay () {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() * 
compchoice.length)];
}

//Player vs Computer
function playRound (playerSelection, computerSelection) {
 if (playerSelection === 'Rock' || computerSelection === 
'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
 } else if (playerSelection === 'Paper' || computerSelection === 
'Rock') 
{
return 'You chose ' + playerSelection + ',' + ' You win!';
 } else if (playerSelection === 'Scissors' || computerSelection === 
'Paper') 
{
return 'You chose ' + playerSelection + ',' + ' You win!';
 } else if (computerSelection === 'Rock' || playerSelection === 
'Scissors') 
{
return 'Computer chose ' + computerSelection + ',' + 'Computer 
wins!';
} else if (computerSelection === 'Paper' || playerSelection === 
'Rock') 
{
return 'Computer chose ' + computerSelection + ',' + 'Computer 
wins!';
} else if (computerSelection === 'Scissors' || playerSelection === 
'Paper') 
{
return 'Computer chose ' + computerSelection + ',' + 'Computer 
wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
}else {
return 'Please chose Rock, Paper, or Scissors';
}
}

const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

It should just play a regular game of rock paper scissors, if i chose rock and the computer chose paper the computer should win. For now i am trying to make it play just one round.

  • 4
    You might consider using consistent indentation when writing code - it'll make reading and debugging it much easier, not only for potential answerers, but for you as well, when we can all see the `{` `}` blocks and their nesting level at a glance, rather than having to carefully pick through each line just to pick up on the logical paths. – CertainPerformance Jul 14 '19 at 21:36
  • Thank you, ill remember that for my next question. – Christopher Altamirano Jul 14 '19 at 22:22
  • Look at the 3 lines at the bottom. You are not using the input value (`person`) anywhere. You are using a `playerSelection` with the value of `'rock'` (with lowercase!) instead to execute the game. Apart from that, the entire code seems flawed, you seem to be confusing basic boolean operators OR (`||`) and AND (`&&`). – Petr Srníček Jul 14 '19 at 22:29

1 Answers1

1

You're using or statements. You need to use and statements. Take for example your first line:

if (playerSelection === 'Rock' || computerSelection === 'Scissors') {
    return 'You chose ' + playerSelection + ',' + ' You win!';
} 

It's saying:

if playerSelection equals Rock OR computerSelection equals Scissors return 

So if playerSelection is Rock it'll return right there. What you need to use is an AND statement instead. Try this:

let playerSelection = prompt("Rock, Paper, Scissors");

// Computer makes a choice
function computerPlay() {
    let compchoice = ['Rock', 'Paper', 'Scissors'];
    return compchoice[Math.floor(Math.random() * compchoice.length)];
}

//Player vs Computer
function playRound(playerSelection, computerSelection) {
    if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
        return 'You chose ' + playerSelection + ',' + ' You win!';
    } else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
        return 'You chose ' + playerSelection + ',' + ' You win!';
    } else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
        return 'You chose ' + playerSelection + ',' + ' You win!';
    } else if (computerSelection === 'Rock' && playerSelection === 'Scissors') {
        return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
    } else if (computerSelection === 'Paper' && playerSelection === 'Rock') {
        return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
    } else if (computerSelection === 'Scissors' && playerSelection === 'Paper') {
        return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
    } else if (computerSelection === playerSelection) {
        return 'Its a draw!';
    } else {
        return 'Please chose Rock, Paper, or Scissors';
    }
}

//const playerSelection = 'Rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

Also there was a typo in your player selection on the third last line. The playerSelection string should be capitalized like the strings it's comparing too.

Jerome
  • 734
  • 1
  • 8
  • 28
  • I tried to change it and it doesnt come up like it did before, but now its just returning the last else statement / 'Please chose Rock, Paper, or Scissors'. Any suggestions? – Christopher Altamirano Jul 14 '19 at 21:54
  • Hmmm, did you copy/paste my code or just make the changes I mentioned? Remember there may still be a typo in playerSelection. – Jerome Jul 14 '19 at 21:59
  • I was using my code before, but i just changed the || to &&. I copied your code and now it works. Not sure what was wrong before. If you can was there anything else wrong with the code other than changing the || to &&??????? – Christopher Altamirano Jul 14 '19 at 22:04
  • There was a typo where you hadn't capitalized the 'R' in 'Rock' on the third last line. – Jerome Jul 14 '19 at 22:06
  • ok thank you. Sorry but i noticed somethings. 1. if i type in random words other than rock paper scissors it still gives me a answer back thats not the last else. 2. when i run it and it promps me to choose something and i type paper it will say computer chose paper comp wins. it is like this for all others as well. What is wrong with it? – Christopher Altamirano Jul 14 '19 at 22:13
  • You've hardcoded the playerSelection to be "Rock" so the program will treat it that way. I'll update my solution. – Jerome Jul 14 '19 at 22:18
  • I tried to take the playerSelection out, it comes back as a reference error on the last console.log and says playerSelection is not defined. – Christopher Altamirano Jul 14 '19 at 22:21
  • No problem. I'm happy to help :-) – Jerome Jul 14 '19 at 22:28
  • Quick question. How would i make so when the user makes a selection he/she can input rock, ROCK, RocK or any other variation) ???? basically case insensitive. – Christopher Altamirano Jul 14 '19 at 22:58
  • I'd set the `playerSelection` and `computerSelection` variables to be upper case then make the comparison. Check [this](https://stackoverflow.com/a/2140644/7918809) out for more info. – Jerome Jul 14 '19 at 23:03