Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. 'use strict';
  2.  
  3. window.alert('コンピュータとジャンケンで対戦します');
  4.  
  5. let win = 0;
  6. let draw = 0;
  7. let lose = 0;
  8.  
  9. for (let i = 1; i <= 10; i++) {
  10. const youStr = window.prompt('グー・チョキ・パーのいずれかを入力して下さい');
  11. let you;
  12. if (youStr == 'グー') {
  13. you = 0;
  14. } else if (youStr == 'チョキ') {
  15. you = 1;
  16. } else if (youStr == 'パー') {
  17. you = 2;
  18. } else {
  19. window.alert('文字列を正しく入力して下さい');
  20. continue;
  21. }
  22.  
  23. const cpu = Math.floor(Math.random() * 3);
  24. let cpuStr;
  25. if (cpu === 0) {
  26. cpuStr = 'グー';
  27. } else if (cpu === 1) {
  28. cpuStr = 'チョキ';
  29. } else /* cpu === 2*/ {
  30. cpuStr = 'パー';
  31. }
  32.  
  33. window.alert('あなたは' + youStr + '、コンピュータは' + cpuStr + 'です');
  34.  
  35. if ((you === 0 && cpu === 1) || (you === 1 && cpu === 2) || (you === 2 && cpu === 0)) {
  36. window.alert('あなたの勝ちです!');
  37. win++;
  38. } else if (you === cpu) {
  39. window.alert('あいこです!');
  40. draw++;
  41. } else {
  42. window.alert('コンピュータの勝ちです!');
  43. lose++;
  44. }
  45. const next = window.confirm('続けますか?');
  46. if (!next) {
  47. break;
  48. }
  49. }
  50. window.alert('あなたは' + win + '勝' + lose + '敗' + draw + '引き分けでした');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement