I'm following a JS course for beginners and the first lesson was about making a "rock, paper, scissors" game.
The instructor said something about this not being the most efficient way to determine the winner but didn't really touch on how to write it. All I can think of is using Switch statement instead of if.
Can someone show me how it should be done?
function getResult(){
if (computerChoice === userChoice) {
result = "It's a draw";
} else if (computerChoice == "rock" && userChoice == "scissors") {
result = 'You lost';
} else if (computerChoice == "rock" && userChoice == "paper") {
result = 'You won!';
} else if (computerChoice == "paper" && userChoice == "scissors") {
result = 'You won';
} else if (computerChoice == "paper" && userChoice == "rock") {
result = 'You lost';
} else if (computerChoice == "scissors" && userChoice == "rock") {
result = 'You won';
} else if (computerChoice == "scissors" && userChoice == "paper") {
result = 'You lost';
}
resultDisplay.innerHTML=result;
}
Thanks, Amin