1
4

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 5 years have passed since last update.

JavaScript 王様ゲーム

Posted at

js.png

##JavaScript勉強メモ。
4点を意識して写経なり
- 動くアプリを作る
- 書いたコードを理解
- 機能またはデザインを追加
- 成果物は、ネット上にあげる

html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>王様ゲーム</title>
</head>
<body>
   <h1>王様ゲーム</h1>
   <p>人数:<input type="text" id="num"></p>
   <p><input type="button" id="btn" value="王様曰く!" onclick="kingSaid();"></p>
   <p id="result"></p>
   
    <script>
        function kingSaid(){
            
            //変数定義
            var orders = [
                "デコピンしなさい",
                "クイズを出しなさい",
                "褒めちぎりなさい",
                "ビンタをしなさい",
            ];
            //乱数に配列を割りあてる★ここがポイント! 配列に乱数を割りあてる
            var order = orders[Math.floor(Math.random() * orders.length)];        
            //人数の入力値を取得
            var num = document.getElementById("num").value;
            //乱数に当てはめる
            var p1 = Math.floor(Math.random() * (num)) + 1;
            var p2 = Math.floor(Math.random() * (num)) + 1;
            
            //入力人数が1人の場合の処理
            if( num < 2){
                p2 = 1;
            
            //p1とp2の重複を避ける処理
            }else{    
               do{
                  p2 = Math.floor(Math.random() * (num)) + 1;
                }while (p1==p2);
            }
            document.getElementById("result").innerHTML = "#"+p1+"の人が"+p2+"の人に"+order;
            
        }
    </script> 
</body>
</html>
1
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?