0
0

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

JavaScript 基礎 サイコロ

Posted at

サイコロ

HTML

<form>
  <input type="button" value="サイコロ" onClick="altRan2()">
  <span id="sai">-</span>
</form> 

JavaScript


function altRan2() {
  var r = Math.floor(Math.random() * 6) + 1;

  document.getElementById('sai').innerHTML = r;
}

Math.floor()

引数として与えた数以下の最大の整数を返します。

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

Math.random()

0–1(0以上、1未満)の範囲で浮動小数点の擬似乱数を返す

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Math/random

element.innerHTML

Element オブジェクトの innerHTML プロパティは、要素内の HTML または XML のマークアップを取得したり設定したりする

https://developer.mozilla.org/ja/docs/Web/API/Element/innerHTML

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?