Skip to content

Instantly share code, notes, and snippets.

@pvanschy
Forked from codecademydev/rockPaperScissors.js
Created January 27, 2021 08:44
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 pvanschy/57fdd101e5024d4ec034d9b63d2f84af to your computer and use it in GitHub Desktop.
Save pvanschy/57fdd101e5024d4ec034d9b63d2f84af to your computer and use it in GitHub Desktop.
Codecademy export
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
return userInput;
}
else{
console.log("Error: Invalid user input");
}
}
console.log(getUserChoice('rock'));
console.log(getUserChoice('potato'));
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
switch(randomNumber){
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
console.log(getComputerChoice());
const determineWinner = (userChoice, computerChoice) => {
if(userChoice === 'bomb'){
return 'CHEAT CODE: User Wins!!!';
}
if(userChoice === computerChoice){
return 'TIE!';
}
if(userChoice === 'rock'){
if(computerChoice === 'paper'){
return 'Computer WON!'
}
else {
return 'You won!'
}
}
if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'Computer WON!'
}
else {
return 'You won!'
}
}
if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'Computer WON!'
}
else {
return 'You won!'
}
}
}
console.log(determineWinner('rock', 'scissors'));
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('paper', 'paper'));
const playGame = () => {
const userChoice = getUserChoice('bomb');
const computerChoice = getComputerChoice('paper');
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