Always getting the same result using if/else statements (Rock, Paper, Scissors)

I think I’m doing something fundamentally wrong with else/if statements.

So - I’m trying to build a simple Rock, Paper, Scissors game . I was happy to see that my random computer selection seemed to work straight away and thought “Finally! I’m getting it JavaScript!!”. However then I was left frustrated by something I thought I understood - the if/else statements.

No matter what the returned results are, I end up getting "looks like a draw!"

Maybe I’m missing something really simple and stupid but I’ve tried it every which way, I’ve reviewed if/else statements on FCC, I’ve googled and I just can’t seem to get it to work.

Also - JavaScript makes me feel very stupid :sweat_smile:

Thanks in advance for anyone who can help me understand what I’m doing wrong :slight_smile:

Code:

// Automated Computer Choice
const choices = ["Rock", "Paper", "Scissors"]
function computerPlay() {
    return choices[Math.floor(Math.random() * Math.floor(choices.length))]
}


//Testing scissors vs random selection
const playerSelection = 'Scissors';
const computerSelection = computerPlay();


// Single Round
function playRound(playerSelection, computerSelection) {

    if (playerSelection == 'Rock' && computerSelection === 'Scissors' || playerSelection == 'Paper' && computerSelection === 'Rock' || playerSelection == 'Scissors' && computerSelection === 'Paper') {
        return "Yay! You win!!";
    }

    else if (playerSelection == 'Rock' && computerSelection === 'Paper' || playerSelection == 'Paper' && computerSelection === 'Scissors' || playerSelection == 'Scissors' && computerSelection === 'Rock') {
        return "Sorry, you lose....";
    }

    else if (playerSelection == computerSelection) {
        return "Looks like a draw!!";
    }

}

// Display player / computer / result in console
console.log("Player chooses: " + playerSelection);
console.log("Computer chooses: " + computerSelection);
console.log(playRound());```

You have a few things going on

  1. You want to use === instead of ==
  2. for multiple conditions, you want if ((condition 1) || (condition 2) || (conditon 3))
  3. You aren’t passing in any arguments to your function console.log(playRound());

When I fix those, your code works great!

Side note: It’s typical to end with } else { ... instead of a condition in this case.

1 Like

You sir, are a gentleman and a scholar! :grin:

I’d tried it formatted a few different ways but the one I had missed everytime was not passing any arguments into console.log(playRound());. I was just calling it. Duhhh.

Thank you!!

1 Like

Ha, we’ve all been there. Glad to help.

Nice! I’ve written this game a couple of times except I did Rock, Paper, Scissors, Lizard, Spock…much more fun.

1 Like