Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

JavaScriptで簡易占いツール作成

概要

JavaScriptで、大吉・小吉・凶を1/3の確率で表示するだけの
シンプルなツールを作ります。

サンプルコード

<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
function outputRandomResult() {
  var random = Math.floor(Math.random()*3)+1;
  outputDiv = document.getElementById('output');
  switch (random) {
  case 1:
    outputDiv.innerText = '大吉';
    break;
  case 2:
    outputDiv.innerText = '小吉';
    break;
  case 3:
    outputDiv.innerText = '凶';
    break;
  }
}

    </script>
  </head>
  <body>
  <input type="button" value="選択" onclick="outputRandomResult();" />
  <hr />
  結果:<span id="output"></span>
  </body>
</html>

10回実行した結果

結果:大吉
結果:小吉
結果:大吉
結果:凶
結果:大吉
結果:小吉
結果:凶
結果:小吉
結果:凶
結果:凶
大吉:3
小吉:3
凶:4

綺麗に分かれた。