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

javascriptでのテトリス作成において必要になった知識をまとめていきます。
#ランダムな整数値の生成
これでmin以上max以下の整数を得られる

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

出典:Math.random()

#多重ループを抜け出す
ラベルをつけることでループを抜け出せる

var grid = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

function validate(value) {
  return value !== 5;
}

grid_loop:
for (var row = 0; row < grid.length; row++) {
  for (var col = 0; col < grid[row].length; col++) {
    var value = grid[row][col];
    console.log(value);
    if (!validate(value)) {
      break grid_loop;
    }
  }
}

出典:JavaScriptで2重ループをbreakする

#配列の中で最大値を見つける

var arr = [1,2,3];
var max = arr.reduce(function(a, b) {
    return Math.max(a, b);
});

出典:Math.max()

##スプレッド構文

var arr = [1, 2, 3];
var max = Math.max(...arr);

出典:Math.max()

スプレッド構文により、イテラブルである配列を展開している

#配列内を検索する
##Array.prototype.includes()
配列内を検索して真偽値を返す

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?