/rockPaperScissors.js Secret
Created
July 19, 2018 13:28
Star
You must be signed in to star a gist
Codecademy export
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') | |
{ | |
return userInput; | |
} else { | |
console.log('Error!'); | |
} | |
} | |
const getComputerChoice = randomNumber => { randomNumber = Math.floor(Math.random() * 3); | |
switch (randomNumber) { | |
case 0: | |
return 'rock'; | |
case 1: | |
return 'paper'; | |
case 2: | |
return 'scissors'; | |
} | |
} | |
function determineWinner (userChoice, computerChoice) { | |
if (userChoice === computerChoice) { | |
return 'game was a tie!'; | |
} | |
if (userChoice === 'rock') { | |
if (computerChoice === 'paper') { | |
return 'computer won!'; | |
} else { | |
return 'user won'; | |
} | |
} | |
} | |
if (userChoice === 'paper') { | |
if (computerChoice === 'scissors') { | |
return 'computer won!'; | |
} else { | |
return 'user won'; | |
} | |
} | |
if (userChoice === 'scissors') { | |
if (computerChoice === 'rock') { | |
return 'computer won'; | |
} else { | |
return 'user won'; | |
} | |
} | |
const playGame = () => { | |
const userChoice = | |
getUserChoice('rock') | |
const computerChoice = getComputerChoice(); | |
console.log(`you threw: ${userChoice}`); | |
console.log(`the computer threw: ${computerChoice}`); | |
console.log(determineWinner(userChoice, computerChoice)); | |
} | |
console.log(getComputerChoice()); | |
playGame(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment