LoginSignup
0
0

More than 3 years have passed since last update.

じゃんけん

Last updated at Posted at 2021-04-02
const GU = 1
const CHOKI = 2
const PA = 3

hand = prompt('コンピュータとじゃんけんをします。半角数字を選んでください。\n\n' + '1: グー\n' + '2: チョキ\n' + '3: パー\n')
hand = parseInt(hand, 10) //数値化



com = Math.floor(Math.random() * 3) + 1 //1.2.3

let result = '';
let comHandName = '';

switch (com) {
  case GU:
    comHandName = 'グー'
    break;

  case CHOKI:
    comHandName = 'チョキ'
    break;

  case PA:
    comHandName = 'パー'
    break;
}

if (hand === com) {
  result = 'あいこです。'
}

else if ((hand == 1 && com == 2) || (hand == 2 && com == 3) || (hand == 3 && com == 1)) {
  result = 'あなたの勝ちです。'
}

else {
  result = 'あなたの負けです。'
}


alert(result + 'コンピューターは「' + comHandName + '」を出しました。')

エラーパターンを除いた場合は、例えば4を入れるとif文のどれにも当てはまらないので負けが返ってくる。
とても勉強になりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0