Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 2, 2020 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/a38d4721fd3adf09d14c617d4ce5d6ea to your computer and use it in GitHub Desktop.
Save codecademydev/a38d4721fd3adf09d14c617d4ce5d6ea to your computer and use it in GitHub Desktop.
Codecademy export
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