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
Thanks in advance for anyone who can help me understand what I’m doing wrong
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());```