Created
November 27, 2016 18:01
-
-
Save mediboinadp/0ca0b1929949cd94c9d79c0e6c4dd8de to your computer and use it in GitHub Desktop.
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
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