Created
October 18, 2017 22:34
-
-
Save khayyamsaleem/433fa2115efd911ced36f19a43b0ffd1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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