I am attempting to make a Rock Paper Scissors game in JavaScript, however, every time I run it the console logs the last option “You win! Scissors beats Paper” even if my selection was something other than scissors. I believe I made an error with my if else statements but maybe the problem comes from some other part.
function getComputerChoice() {
const randomNumber = Math.floor(Math.random()*3);
switch(randomNumber) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
};
}
function getPlayerChoice() {
prompt("rock paper or scissors?").toLowerCase();
}
function playRound (playerSelection,computerSelection) {
computerSelection = getComputerChoice()
playerSelection = getPlayerChoice()
if (computerSelection === playerSelection) {
console.log ("Tie");
}
else if (computerSelection === "rock" && playerSelection === "scissors") {
console.log ("You lose! Rock beats Scissors");
}
else if (computerSelection === "paper" && playerSelection === "rock") {
console.log ("You lose! Paper beats Rock");
}
else if (computerSelection === "scissors" && playerSelection === "paper") {
console.log ("You lose! Scissors beats Paper");
}
else if (playerSelection === "rock" && computerSelection === "scissors") {
console.log ("You win! Rock beats Scissors");
}
else if (playerSelection === "paper" && computerSelection === "rock") {
console.log ("You win! Paper beats Rock");
}
else (playerSelection === "scissors" && computerSelection === "paper") ;{
console.log ("You win! Scissors beats Paper");
}
}