Traverse a 2d array from the centre in a spiral
npm install spiral-arrayjs
const array = generateArray(3);
printArrayTable(array)
``java
Traversing 9 cells
āāāāāāāāāāā¬āāāā¬āāāā¬āāāā
ā (index) ā 0 ā 1 ā 2 ā
āāāāāāāāāāā¼āāāā¼āāāā¼āāāā¤
ā 0 ā 1 ā 2 ā 3 ā
ā 1 ā 4 ā 5 ā 6 ā
ā 2 ā 7 ā 8 ā 9 ā
āāāāāāāāāāā“āāāā“āāāā“āāāā
`$3
`js
const centre = locateCentre(array)
``java
centre { contents: 5, coordinates: [ 1, 1 ] }
`
$3
`js
const results = spiral(array);
``java
results {
contents: [
5, 6, 3, 2, 1,
4, 7, 8, 9
],
coordinates: [
[ 1, 1 ], [ 1, 2 ],
[ 0, 2 ], [ 0, 1 ],
[ 0, 0 ], [ 1, 0 ],
[ 2, 0 ], [ 2, 1 ],
[ 2, 2 ]
]
}
`$3
`js
const cell = nextCell(array, [0,2], 'l');
``java
cell {
contents: [2],
coordinates: [ [ 0, 1 ] ]
}
`$3
#####
type
There are two types of response that can be returned from some functions.1. Contents of the cell
2. Coordinates of the cell
If
type is not specified, both contents and coordinates will be returned.You can import these functions into your project:
`js
module.exports = {
spiral: (array, type) => traverseArray(array, type),
generateArray: (size) => generateArray(size),
printArrayTable: (array) => console.table(array),
locateCentre: (array, type) => locateCentre(array, type),
nextCell: (array, currentCell, direction, type) => nextCell(array, currentCell, direction, type)
};
`#### ā¹ļø Help
#####
Node.js heap out of memory`When dealing with very large arrays (around 1000 x 1000 or higher) more memory can be allocated to your node process. See: https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory