-
-
Save codecademydev/5b1702f73a71b20cac66b95776e643c0 to your computer and use it in GitHub Desktop.
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 = () => { | |
const randomNumber = Math.floor(Math.random() * 3); | |
switch(randomNumber){ | |
case 0: | |
return 'rock'; | |
case 1: | |
return 'paper'; | |
case 2: | |
return 'scissors'; | |
} | |
}; | |
const determineWinner = (userChoice, computerChoice) => { | |
if (userChoice === computerChoice){ | |
return 'Tie' | |
} | |
if (userChoice === 'rock' && computerChoice === 'paper') { | |
return 'You lost' | |
} else{ | |
return 'You won' | |
} | |
if (userChoice === 'paper' && computerChoice === 'scissors') { | |
return 'You lost' | |
} else{ | |
return 'You won' | |
} | |
if (userChoice === 'scissors' && computerChoice ==='rock') { | |
return 'You lost' | |
} else{ | |
return 'You won' | |
} | |
} | |
const playGame = () => { | |
const userChoice = getUserChoice('paper'); | |
const computerChoice = getComputerChoice(); | |
console.log(`You threw ${userChoice}`); | |
console.log(`Computer threw ${computerChoice}`); | |
console.log(determineWinner()); | |
}; | |
playGame() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment