Skip to content

Instantly share code, notes, and snippets.

@TheBrotherFromASouthernMother
Last active March 12, 2020 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheBrotherFromASouthernMother/23498754d63f572f1bfd54faccbb32b0 to your computer and use it in GitHub Desktop.
Save TheBrotherFromASouthernMother/23498754d63f572f1bfd54faccbb32b0 to your computer and use it in GitHub Desktop.
Create Grid Javascript
// uses es6 syntax but can swap out the "lets" and "consts" if you need to use es5
function createGrid(numberOfRowsAndColumns) {
const grid = [];
for (let i = 0; i < numberOfRowsAndColumns; i++) { // upper for-loop creates the rows
const row = [];
for (let j = 0; j < numberOfRowsAndColumns; j++) { / lower for loop creates the individual columns
const columnValue = Math.floor(Math.random() * 100); // generates a random value between 1 and 100
row.push(columnValue);
}
grid.push(row);
}
return grid;
}
const newGrid = createGrid(5);
/*
Should create a grid (2D array) looking something like:
[ [ 46, 12, 77, 86, 19 ],
[ 52, 33, 40, 74, 24 ],
[ 9, 61, 42, 14, 35 ],
[ 84, 37, 55, 88, 88 ],
[ 48, 56, 76, 52, 89 ] ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment