Skip to content

Instantly share code, notes, and snippets.

@khayyamsaleem
Created October 18, 2017 22:34
Show Gist options
  • Save khayyamsaleem/433fa2115efd911ced36f19a43b0ffd1 to your computer and use it in GitHub Desktop.
Save khayyamsaleem/433fa2115efd911ced36f19a43b0ffd1 to your computer and use it in GitHub Desktop.
var userInput = 'Rock'
userInput = userInput.toLowerCase();
function getComputerChoice() {
var randomNumber = Math.floor(Math.random() * 3)
switch (randomNumber){
case 0:
return 'rock'
case 1:
return 'paper'
case 2:
return 'scissors'
}
}
function determineWinner (userChoice, computerChoice){
if (userChoice === computerChoice){
return 'The game was 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!"
}
}
}
}
function playGame() {
var userChoice = userInput
var 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