Advertisement
Guest User

javasccript

a guest
Apr 24th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. const ICON = {
  2. 'tie': 'info',
  3. 'loss': 'error',
  4. 'win': 'success'
  5. }
  6. $(function() {
  7. $('.gameicons > i').click(function(e) {
  8.  
  9. // Getting user choice
  10. var userPick = $(e.currentTarget).data('val')
  11. console.log(userPick)
  12.  
  13. //Deciding computer choice
  14. var computerChoice = Math.random();
  15. if (computerChoice < 0.34) {
  16. computerChoice = "rock";
  17. } else if (computerChoice <= 0.67) {
  18. computerChoice = "paper";
  19. } else {
  20. computerChoice = "scissors";
  21. }
  22.  
  23. var win = decideWinner(userPick, computerChoice)
  24.  
  25. swal(win, "The computer chose " + computerChoice, ICON[win])
  26.  
  27. })
  28. })
  29.  
  30. function decideWinner(user, bot) {
  31. switch (user) {
  32. case "paper":
  33. if (bot == "paper") return 'tie'
  34. if (bot == "scissors") return 'loss'
  35. if (bot == "rock") return 'win'
  36. break
  37. case "rock":
  38. if (bot == "paper") return 'loss'
  39. if (bot == "scissors") return 'win'
  40. if (bot == "rock") return 'tie'
  41. break
  42. case "scissors":
  43. if (bot == "paper") return 'win'
  44. if (bot == "scissors") return 'tie'
  45. if (bot == "rock") return 'loss'
  46. break;
  47. default:
  48. return 'An Error Occured'
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement