I'm trying to create a function which generates a board.
function createBoard(rowSize, colSize, mineNum) {
let board = { cells: []}
let boardSize = rowSize * colSize;
for (let i = 0; i < boardSize; i++){
for (let j = 0; j < rowSize; j++){
board.cells[i] = {
row: i%rowSize,
col: i%j,
isMine: false,
isMarked: false,
hidden: true
}
}
}
return board
}
The current properties for rows and columns are just wild guessed. How would I create a board where all the column and row properties are filled out correctly? As in, no two cells would have the same row and column properties.
Cheers!
You're not too far off! You don't need to multiple columns and rows because your nested loop is actually taking care of that for you. You can just use i
and j
directly and push to the cells
array.
function createBoard(rowSize, colSize, mineNum) {
const board = { cells: [] };
for (let i = 0; i < colSize; i++){
for (let j = 0; j < rowSize; j++){
board.cells.push({
row: j,
col: i,
isMine: false,
isMarked: false,
hidden: true
});
}
}
return board;
}
const board = createBoard(10, 10);
console.log(board);