HTML勉強メモ

HTMLやJavaScriptなどのことについて書いています。

おみくじの結果をalertで出力する方法

おみくじの結果をalertで出力する方法について

別記事で作成したおみくじの結果をalertで出力する。
別記事
htmlst.hatenablog.jp

javascript内で取得した乱数に対応したif文の条件の結果をalertで表示する。

<form><input type="button" value="おみくじ" onClick="random()"> </form>

<script type="text/javascript">
function random() {
var rand = Math.floor( Math.random() * 5) ; //おみくじの目の生成
        if (rand == 0) msg = "大吉";
	if (rand == 1) msg = "中吉";
	if (rand == 2) msg = "小吉";
	if (rand == 3) msg = "吉";
	if (rand == 4) msg = "凶";
    alert(msg); //おみくじの目の出力
}
</script>

表示例


まとめ

JavaScript内で取得したおみくじの結果はalertでも出力することができる。

関連記事
htmlst.hatenablog.jp
htmlst.hatenablog.jp
htmlst.hatenablog.jp
htmlst.hatenablog.jp