Created
September 9, 2016 16:18
-
-
Save rohan-paul/830d928481154ac60787b1fb96e12ede to your computer and use it in GitHub Desktop.
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
To store a grid of values, we have several options. We can use an array of row arrays and use two property accesses to get to a specific square, like this: | |
var grid = [["top left", "top middle", "top right"], ["bottom left", "bottom middle", "bottom right"]]; | |
console.log(grid[1][2]); | |
// . bottom right | |
Or we can use a single array, with size width × height, and decide that | |
the element at (x,y) is found at position x + (y × width) in the array. | |
var grid = ["top left", "top middle", "top right", "bottom left", "bottom middle", "bottom right"]; | |
console.log(grid[2 + (1 * 3)]); | |
// . bottom right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment