LoginSignup
2
1

More than 3 years have passed since last update.

if文なしでじゃんけん javascript

Posted at

if文なしでじゃんけん - Qiita を読んで、自分ならどうするか考えてみた。

解答

const battle = {
    "グー": {
        "グー": "あいこ",
        "チョキ": "あなたの勝ち",
        "パー": "あなたの負け",
    },
    "チョキ": {
        "グー": "あなたの負け",
        "チョキ": "あいこ",
        "パー": "あなたの勝ち",
    },
    "パー": {
        "グー": "あなたの勝ち",
        "チョキ": "あなたの負け",
        "パー": "あいこ",
    },
}

const battleResult = (myHand, yourHand) =>
    `[${myHand}] vs [${yourHand}] => ${battle[myHand][yourHand]}`

console.log(battleResult("グー", "チョキ"))
console.log(battleResult("グー", "グー"))
console.log(battleResult("グー", "パー"))

可読性 MAX !!

結果

[グー] vs [チョキ] => あなたの勝ち
[グー] vs [グー] => あいこ
[グー] vs [パー] => あなたの負け
2
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
2
1