Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getUserChoice() {
- var userInput = prompt('Rock, Paper, or Scissors?');
- userInput = userInput.toLowerCase();
- if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
- return userInput;
- } else {
- console.log('Error!');
- }
- }
- function getComputerChoice() {
- var randomNumber = Math.floor(Math.random() * 3);
- switch (randomNumber) {
- case 0:
- return 'rock';
- case 1:
- return 'paper';
- case 2:
- return 'scissors';
- break;
- }
- }
- function determineWinner(userChoice, computerChoice) {
- if (userChoice === computerChoice) {
- return 'Tie Game';
- } else {
- if (userChoice === 'rock') {
- if (computerChoice ==='paper') {
- return 'Computer wins.';
- } else {
- return 'You win!';
- }
- } else {
- if (userChoice === 'paper') {
- if (computerChoice === 'scissors') {
- return 'Computer wins.';
- } else {
- return 'You win!';
- }
- } else {
- if (userChoice === 'scissors') {
- if (computerChoice === 'rock') {
- return 'Computer wins.';
- } else {
- return 'You win!';
- }
- }
- }
- }
- }
- }
- function playGame() {
- var userChoice = getUserChoice();
- var computerChoice = getComputerChoice();
- console.log('You threw: ' + userChoice);
- console.log('The computer threw: ' + computerChoice);
- console.log(determineWinner(userChoice, computerChoice));
- }
- playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement