-
-
Save codecademydev/d04bee496d77190ade9855343d1a3e62 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
const getUserChoice = (userInput) => { | |
userInput = userInput.toLowerCase(); | |
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { | |
return userInput; | |
} else { | |
console.log('Error! Please try writing: rock, paper or scissors'); | |
} | |
} | |
function getComputerChoice() { | |
let randomNumber = Math.floor(Math.random() * 3); | |
switch (randomNumber) { | |
case 0: | |
return 'rock'; | |
break; | |
case 1: | |
return 'paper'; | |
break; | |
case 2: | |
return 'scissors'; | |
break; | |
} | |
} | |
function determineWinner (userChoice,computerChoice) { | |
if (userChoice === computerChoice) { | |
console.log("It's a tie!"); | |
} else { | |
if (userChoice === 'rock') { | |
{ | |
if (computerChoice === 'paper') { | |
return console.log('Computer won!'); | |
} else { | |
return console.log('User won!'); | |
} | |
} | |
} | |
if (userChoice === 'paper') { | |
{if (computerChoice === 'scissors') { | |
return console.log('Computer won!'); | |
} else { | |
return console.log('User won!'); | |
} | |
} | |
} | |
if (userChoice === 'scissors') { | |
{if (computerChoice === 'rock') { | |
return console.log('Computer won!'); | |
} else { | |
return console.log('User won!'); | |
} | |
} | |
} | |
} | |
} | |
function playGame () { | |
const userChoice = getUserChoice('paper'); | |
console.log('You threw '+ userChoice); | |
const computerChoice = getComputerChoice(); | |
console.log('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