Skip to content

Instantly share code, notes, and snippets.

@mediboinadp
Created November 27, 2016 18:01
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 mediboinadp/0ca0b1929949cd94c9d79c0e6c4dd8de to your computer and use it in GitHub Desktop.
Save mediboinadp/0ca0b1929949cd94c9d79c0e6c4dd8de to your computer and use it in GitHub Desktop.
function getUserChoice() {
userInput = prompt("enter your choice? ");
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else if (userInput === 'bomb') {
console.log('you won!');
} else {
console.log('Error!');
}
}
function getComputerChoice() {
randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
default:
console.log('Error');
}
}
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 === 'rock' || computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock' || computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
}
function playGame() {
var userChoice=getUserChoice();
var computerChoice=getComputerChoice();
onsole.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
conslole.log(determineWinner(userChoice, computerChoice));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment