Last active
March 12, 2020 00:31
-
-
Save TheBrotherFromASouthernMother/23498754d63f572f1bfd54faccbb32b0 to your computer and use it in GitHub Desktop.
Create Grid Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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