Advertisement
autOzun

Untitled

Jan 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. function getUserChoice() {
  2. var userInput = prompt('Rock, Paper, or Scissors?');
  3.  
  4. userInput = userInput.toLowerCase();
  5.  
  6. if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
  7. return userInput;
  8. } else {
  9. console.log('Error!');
  10. }
  11. }
  12.  
  13. function getComputerChoice() {
  14. var randomNumber = Math.floor(Math.random() * 3);
  15. switch (randomNumber) {
  16. case 0:
  17. return 'rock';
  18. case 1:
  19. return 'paper';
  20. case 2:
  21. return 'scissors';
  22. break;
  23. }
  24. }
  25.  
  26. function determineWinner(userChoice, computerChoice) {
  27. if (userChoice === computerChoice) {
  28. return 'Tie Game';
  29. } else {
  30. if (userChoice === 'rock') {
  31. if (computerChoice ==='paper') {
  32. return 'Computer wins.';
  33. } else {
  34. return 'You win!';
  35. }
  36. } else {
  37. if (userChoice === 'paper') {
  38. if (computerChoice === 'scissors') {
  39. return 'Computer wins.';
  40. } else {
  41. return 'You win!';
  42. }
  43. } else {
  44. if (userChoice === 'scissors') {
  45. if (computerChoice === 'rock') {
  46. return 'Computer wins.';
  47. } else {
  48. return 'You win!';
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55.  
  56. function playGame() {
  57. var userChoice = getUserChoice();
  58. var computerChoice = getComputerChoice();
  59. console.log('You threw: ' + userChoice);
  60. console.log('The computer threw: ' + computerChoice);
  61. console.log(determineWinner(userChoice, computerChoice));
  62. }
  63.  
  64. playGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement