DigtalBloomStudio

ランダムな数字サイコロ

top画像

ランダムな数字サイコロ

ページを何度かリロールすることで数字が変化するよ

一般的な書き方

  
  <script>
  const dice = Math.floor(Math.random() * 6) + 1;
  document.write(dice);
  </script>
  

(1行コードの場合)関数での書き方

  
  <script>
                                        *=掛け算
  const getDicer = () => {        *(数字をランダムに出す関数)
  return Math.floor(Math.random() * 6) + 1;
  }
  const dice1 = getDicer();
  document.write(dice1);        1サイコロ数字
  document.write('<br>');       改行用
  document.write(getDicer());   2サイコロ数字
  </script>