Created
February 26, 2023 19:41
-
-
Save pochito427/95887af5e93ecf000b45bb947a4cecda to your computer and use it in GitHub Desktop.
Rock, paper, scissors game with switch declaration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateGame(user, cpu){ | |
switch(true){ | |
case (user === cpu): | |
alert("Tie!"); | |
break; | |
case (user === 2 && cpu === 3): | |
alert("CPU wins, sorry!"); | |
break; | |
case (user === 1 && cpu === 2): | |
alert("CPU wins, sorry!"); | |
break; | |
case (user === 3 && cpu === 1): | |
alert("CPU wins, sorry!"); | |
break; | |
case (user < 1 || user > 3): | |
alert("Your choice must be between 1 and 3!"); | |
break; | |
default: | |
alert("You win!"); | |
} | |
} | |
let userInput = prompt("Please choose a number between 1 and 3: 1 for rock, 2 for paper, 3 for scissors"); | |
let user = Number(userInput); | |
let cpu = Math.floor((Math.random() * 3) + 1); | |
validateGame(user, cpu); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment