-
-
Save codecademydev/a38d4721fd3adf09d14c617d4ce5d6ea 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('Invalid option'); | |
} | |
}; | |
console.log(getUserChoice('paper')); | |
const getComputerChoice = () => { | |
const randomNumber = Math.floor(Math.random() * 3); | |
if (randomNumber === 0) { | |
return 'rock'; | |
} else if (randomNumber === 1) { | |
return 'paper'; | |
} else if (randomNumber === 2) { | |
return 'scissors'; | |
} | |
}; | |
console.log(getComputerChoice()) | |
function determineWinner(userChoice, computerChoice) { | |
if (userChoice === computerChoice) { | |
return 'The game is a tie'; | |
} | |
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')); // prints something like 'The computer won!' | |
console.log(determineWinner('paper', 'paper')); // prints something like 'The game is a tie!' | |
console.log(determineWinner('paper', 'rock')); // prints something like 'The user won!*/ | |
function playGame() { | |
const userChoice = getUserChoice('paper'); | |
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