-
-
Save codecademydev/a234d5cd225afc6414a305c4c2bd172e 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, invalid term entered.'); | |
} | |
}; | |
const getComputerChoice = () => { | |
let randomNumber = Math.floor(Math.random() * 3); | |
if (randomNumber === 0) { | |
return 'rock'; | |
} else if (randomNumber === 1) { | |
return 'paper'; | |
} else if (randomNumber === 2) { | |
return 'scissors'; | |
} else { | |
return 'error'; | |
} | |
}; | |
const determineWinner = (userChoice, computerChoice) => { | |
if (userChoice === computerChoice) { | |
return 'The game is a tie!'; | |
} | |
if (userChoice === 'rock') { | |
if (computerChoice === 'paper') { | |
return 'You lose!'; | |
} else { | |
return 'You win!'; | |
} else if (userChoice === 'paper') { | |
if (computerChoice === 'rock') { | |
return 'You win!'; | |
} else { | |
return 'You lose!'; | |
} else if (userChoice === 'scissors') { | |
if (computerChoice === 'rock') { | |
return 'You lose!'; | |
} else { | |
return 'You win!'; | |
} | |
} else { | |
return 'error'; | |
} | |
}; | |
console.log(determineWinner('rock', 'paper')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment