Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <title>Rock Paper Scissors Game</title>
  4. <head>
  5. <script>
  6. const getUserChoice = userInput => {
  7. userInput = userInput.toLowerCase();
  8. if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
  9. return userInput;
  10. }
  11. return 'error';
  12. }
  13.  
  14. function getComputerChoice() {
  15. var rand = Math.floor(Math.random() * 4);
  16. var res = '';
  17. switch(rand) {
  18. case 1:
  19. res = 'rock';
  20. break;
  21. case 2:
  22. res = 'paper';
  23. break;
  24. case 3:
  25. res = 'scissors';
  26. break;
  27. default:
  28. res = "scissors";
  29. break;
  30. }
  31. return res;
  32. }
  33. function determineWinner(userChoice, computerChoice) {
  34. if(userChoice === computerChoice) {
  35. return 'Game was a tie.';
  36. }
  37. if(userChoice === 'rock') {
  38. if(computerChoice === 'paper') {
  39. return 'Computer won';
  40. } else {
  41. return 'user won';
  42. }
  43. }
  44. if(userChoice === 'paper') {
  45. if(computerChoice === 'scissors') {
  46. return 'computer won';
  47. } else {
  48. return 'user won';
  49. }
  50. }
  51. if(userChoice == 'scissors') {
  52. if(computerChoice === 'rock') {
  53. return 'computer won';
  54. } else {
  55. return 'user won';
  56. }
  57. }
  58. if(userChoice === 'bomb') {
  59. return 'user won';
  60. }
  61. }
  62.  
  63. function playGame() {
  64. var userChoice = getUserChoice('scissors');
  65. var computerChoice = getComputerChoice();
  66. console.log("User: " + userChoice);
  67. console.log("Computer: " + computerChoice);
  68. console.log(determineWinner(userChoice, computerChoice));
  69. }
  70. playGame();
  71. </script>
  72. </head>
  73. <body>
  74. </body>
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement