-
-
Save codecademydev/8c3244ab77c1e28e614f2950a82c020a 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('Wrong input!'); | |
} | |
} | |
const getComputerChoice = () => { | |
switch(Math.floor(Math.random() * 3)) { | |
case 0: | |
return 'rock'; | |
case 1: | |
return 'paper'; | |
case 2: | |
return 'scissors'; | |
} | |
} | |
const determineWinner = (userChoice, computerChoice) => { | |
if (userChoice === computerChoice) { | |
return 'This game is a tie'; | |
} | |
} | |
// let userChoice = 'rock'; | |
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'; | |
} | |
} | |
console.log(determineWinner('paper', 'scissors')); | |
const playGame = () => { | |
const userChoice = getUserChoice('scissors'); | |
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