I create a sapper on JS, there is a two-dimensional array filled with zeros and ones, 1 is a mine, you need to create an array filled with numbers, the number of mines around the cell, you can’t do because of the array boundaries.
var mineField = [ ['0', '0', '0', '1', '0'], ['0', '1', '0', '0', '0'], ['0', '0', '1', '0', '1'], ['1', '0', '0', '1', '0'], ['0', '0', '1', '0', '0'] ]; var markField=[]; for(var i = 0;i< mineField.length;i++){ markField[i] = []; for(var j = 0; j< mineField[i].length; j++){ if(field[i][j] == 1) markField[i][j] = "-1";//если мина то вносить -1 else{ var mineCounter = 0;//Количество мин вокруг ячейки if (field[i-1][j-1] === 1) mineCounter++; if (field[i-1][j] === 1) mineCounter++; if (field[i-1][j+1] === 1) mineCounter++; if (field[i][j-1] === 1) mineCounter++; if (field[i][j+1] === 1) mineCounter++; if (field[i+1][j-1] === 1) mineCounter++; if (field[i+1][j] === 1) mineCounter++; if (field[i+1][j+1] === 1) mineCounter++; markField[i][j] = mineCounter; } } }