Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 10, 2017 07:18
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/7effa10a8345a2b695f5e658159f263b to your computer and use it in GitHub Desktop.
Save codecademydev/7effa10a8345a2b695f5e658159f263b to your computer and use it in GitHub Desktop.
Codecademy export
function getUserChoice(){
var userInput = prompt('Whats is your choice?: rock, paper, or scissors.');
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
return userInput;
} else {
console.log('error');
}
}
function getComputerChoice(){
var ramdonNumber = Math.floor(Math.random()*3);
switch (ramdonNumber){
case 0:
return 'rock';break;
case 1:
return 'paper';break;
case 2:
return 'scissors';break;
}
}
function determineWinner(userChoice, computerChoice){
if (userChoice === computerChoice){
return 'the game is a tie';
}
if (userChoice === 'rock') {
if (computerChoice === 'scissors') {
return 'user won';
} else {
return 'computer 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';
}
}
}
function playGame(){
var userChoice = getUserChoice();
var computerChoice = getComputerChoice();
console.log('You choice: ' + userChoice);
console.log('Computer choice: ' +
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