Advertisement
Guest User

Untitled

a guest
Oct 25th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. const getUserChoice = userInput => {
  2. userInput = userInput.toLowerCase();
  3. if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
  4. return userInput;
  5. } else {
  6. console.log('Error!');
  7. }
  8.  
  9. };
  10.  
  11. function getComputerChoice() {
  12. const randomNumber = Math.floor(Math.random() * 3);
  13. switch (randomNumber) {
  14. case 0:
  15. return 'rock';
  16. case 1:
  17. return 'paper';
  18. case 2:
  19. return 'scissors';
  20. }
  21. };
  22.  
  23. const determineWinner = (userChoice, computerChoice) => {
  24. if (userChoice === computerChoice) {
  25. return 'The game was a tie!'
  26. }
  27. if (userChoice === 'rock'){
  28. if (computerChoice === 'paper'){
  29. return 'The computer won!'
  30. } else {
  31. return 'The player won!'
  32. }
  33.  
  34. }
  35. if (userChoice === 'paper'){
  36. if (computerChoice === 'scissors'){
  37. return 'The computer won!'
  38. } else {
  39. return 'The player won!'
  40. }
  41. }
  42. if (userChoice === 'scissors'){
  43. if (computerChoice === 'rock'){
  44. return 'The computer won!'
  45. } else {
  46. return 'The player won!'
  47. }
  48. }
  49. }
  50.  
  51. console.log(determineWinner('paper', 'scissors'));
  52. console.log(determineWinner('paper', 'paper'));
  53. console.log(determineWinner('paper', 'rock'));
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement