Created
February 6, 2021 02:56
-
-
Save jloescher/a7412a198acc1651b79f9e408abe5649 to your computer and use it in GitHub Desktop.
Simple CLI rock, paper, scissors game.
This file contains hidden or 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
const userInput = 'Bomb'.toLowerCase(); | |
function getComputerChoice() { | |
let randomInt = Math.floor(Math.random() * 3); | |
switch (randomInt) { | |
case 0: | |
return 'rock'; | |
break; | |
case 1: | |
return 'paper'; | |
break; | |
case 2: | |
return 'scissors'; | |
break; | |
} | |
} | |
function getUserChoice(userInput) { | |
if ( | |
userInput === 'rock' || | |
userInput === 'paper' || | |
userInput === 'scissors' || | |
userInput === 'bomb' | |
) { | |
return userInput; | |
} else { | |
console.log('Please enter either rock, paper, or scissors'); | |
} | |
} | |
function determineWinner(userChoice, computerChoice) { | |
if (userChoice === computerChoice) { | |
return 'The game was a tie.'; | |
} else if (userChoice === 'bomb') { | |
return `The user wins! The ${userChoice} kills all!`; | |
} else { | |
if (userChoice === 'rock') { | |
if (computerChoice === 'paper') { | |
return `${computerChoice} covers ${userChoice}, computer wins!`; | |
} else { | |
return `${userChoice} smashes ${computerChoice}, user wins!`; | |
} | |
} else if (userChoice === 'paper') { | |
if (computerChoice === 'scissors') { | |
return `${computerChoice} cuts ${userChoice}, computer wins!`; | |
} else { | |
return `${userChoice} covers ${computerChoice}, user wins!`; | |
} | |
} else if (userChoice === 'scissors') { | |
if (computerChoice === 'rock') { | |
return `${computerChoice} smashes ${userChoice}, computer wins!`; | |
} else { | |
return `${userChoice} cuts ${computerChoice}, user wins!`; | |
} | |
} | |
} | |
} | |
function playGame() { | |
console.log(determineWinner(getUserChoice(userInput), getComputerChoice())); | |
} | |
playGame(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment