Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getUserChoice = userInput =>{
- userInput = userInput.toLowerCase()
- if (userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "bomb") {
- return userInput;
- } else {
- console.log("An error has occurred.");
- }
- };
- const getComputerChoice = () =>{
- switch (Math.floor(Math.random() * 2.99999)){
- case 0:
- return "rock"
- break;
- case 1:
- return "paper"
- break;
- case 2:
- return "scissors"
- break;
- default:
- return null
- }
- }
- const determineWinner = (userChoice, computerChoice) => {
- if (userChoice === computerChoice){
- return "The game is a tie.";
- } else if (userChoice === 'rock') {
- if (computerChoice === 'paper') {
- return "Computer wins.";
- } else {
- return "Player wins."
- }
- } else if (userChoice === 'paper') {
- if (computerChoice === 'scissors') {
- return "Computer wins.";
- } else {
- return "Player wins.";
- }
- } else if (userChoice === 'scissors') {
- if (computerChoice === 'rock') {
- return "Computer wins.";
- } else {
- return "Player wins.";
- }
- } else if (userChoice === 'bomb') {
- return "Player activated cheat codes; Player wins"
- }
- }
- const playGame = () => {
- let userChoice = getUserChoice('ROCK');
- let computerChoice = getComputerChoice();
- console.log(`You threw ${userChoice}.`)
- console.log(`Computer threw ${computerChoice}.`)
- console.log(determineWinner(userChoice, computerChoice))
- }
- playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement