-
-
Save codecademydev/7effa10a8345a2b695f5e658159f263b to your computer and use it in GitHub Desktop.
Codecademy export
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(){ | |
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