1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javascript じゃんけんゲーム

Posted at

const GU = 1;
const CHOKI = 2;
const PA = 3;

let hand = prompt('半角数字で1~3の数字を入力してください。\n\n' + GU + ':グー \n' + CHOKI + ':チョキ \n' + PA + ':パー \n');

hand = parseInt(hand, 10);

if (hand !== GU && hand !== CHOKI && hand !== PA) {
alert('入力値をうまく認識できませんでした。ブラウザを再読み込みすると、もう一度挑戦できます。');
}
else {
let com = Math.floor(Math.random() * 3) + 1;

let comHandName = '';
switch (com) {
case GU:
comHandName = 'グー';
break;
case CHOKI:
comHandName = 'チョキ';
break;
case PA:
comHandName = 'パー';
break;
}

let msgResult = '';
if (hand === com) {
msgResult = '結果はあいこでした';
}
else if ((hand === GU && com === CHOKI) || (hand === CHOKI && com === PA) || (hand === PA && com === GU)) {
msgResult = '勝ちました';
}
else {
msgResult = '負けました';
}

msgResult = msgResult + 'コンピュータの出した手は「' + comHandName + '」でした';
alert(msgResult);
}

自作してみたので記念に残します。
switchを使う際は「break」を忘れないようにしないとですね。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?