Created
December 10, 2017 21:20
-
-
Save SebGogo/f765b24e2cfdf65c9ef3a0d39e9c45b2 to your computer and use it in GitHub Desktop.
Codecademy - Rock, Scissors, Paper Project
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
const getUserChoice = (userInput) => { | |
userInput = userInput.toLowerCase(); | |
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') { | |
return userInput; | |
} | |
else { | |
console.log('Invalid Option'); | |
} | |
}; | |
function getComputerChoice () { | |
randomNumber = Math.floor(Math.random() * 3); | |
switch (randomNumber) { | |
case 0: | |
return 'rock'; | |
break; | |
case 1: | |
return 'scissors'; | |
break; | |
case 2: | |
return 'paper'; | |
break; | |
} | |
} | |
function determineWinner (userChoice, computerChoice) { | |
if (userChoice === computerChoice) { | |
return 'Tie game!'; | |
} | |
if (userChoice === 'rock') { | |
if (computerChoice === 'paper') { | |
return 'The computer won!'; | |
} else { | |
return 'You won!'; | |
} | |
} | |
if (userChoice === 'paper') { | |
if (computerChoice === 'scissors') { | |
return 'The computer won!'; | |
} else { | |
return 'You won!'; | |
} | |
} | |
if (userChoice === 'scissors') { | |
if (computerChoice === 'rock') { | |
return 'The computer won!'; | |
} else { | |
return 'You won!'; | |
} | |
} | |
if (userChoice === 'bomb') { | |
return 'You won!'; | |
} | |
} | |
const playGame = () => { | |
const userChoice = getUserChoice('bomb'); | |
const computerChoice = getComputerChoice(); | |
console.log(`You threw ${userChoice}`); | |
console.log(`The computer threw ${computerChoice}`); | |
console.log(determineWinner(userChoice, computerChoice)); | |
} | |
playGame(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment