Guest User

Untitled

a guest
Nov 19th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 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. const getComputerChoice = () => {
  11. let randomNumber = Math.random();
  12. if (randomNumber < 0.33) {
  13. return 'rock';
  14. } else if (randomNumber > 0.34 && randomNumber < 0.66) {
  15. return 'paper';
  16. } else if (randomNumber > 0.67) {
  17. return 'scissors';
  18. }
  19. };
  20.  
  21. const determineWinner = (userChoice, computerChoice) => {
  22. if (userChoice === computerChoice) {
  23. return 'tie';
  24. }
  25. if (userChoice === 'rock') {
  26. if (computerChoice === 'scissors') {
  27. return 'user won';
  28. } else {
  29. return 'computer won';
  30. }
  31. }
  32. if (userChoice === 'paper') {
  33. if (computerChoice === 'rock') {
  34. return 'user won';
  35. } else {
  36. return 'computer won';
  37. }
  38. }
  39. if (userChoice === 'scissors') {
  40. if (computerChoice === 'paper') {
  41. return 'user won';
  42. } else {
  43. return 'computer won';
  44. }
  45. }
  46.  
  47. };
  48.  
  49. const playGame = () => {
  50. const userChoice = getUserChoice('scissors');
  51. const computerChoice = getComputerChoice();
  52. console.log('user threw: ' + userChoice);
  53. console.log('computer threw: ' + computerChoice);
  54. console.log(determineWinner(userChoice, computerChoice));
  55. };
  56.  
  57. playGame();
Add Comment
Please, Sign In to add comment