-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstumble.js
More file actions
26 lines (23 loc) · 787 Bytes
/
Copy pathstumble.js
File metadata and controls
26 lines (23 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module.exports = function(columns, rows) {
return (origin, direction) => {
let delta = 0;
direction = ["NE", "NW", "SW", "SE"][direction]
// if DIRECTION is EAST and ORIGIN is NOT in last column
if (direction[1] === "E" && origin % columns !== columns - 1) {
delta += 1
}
// if DIRECTION is WEST and ORIGIN is NOT in first column
else if (direction[1] === "W" && origin % columns !== 0) {
delta -= 1
}
// if DIRECTION is SOUTH and ORIGIN is NOT in last row
if (direction[0] === "S" && origin / columns < rows - 1) {
delta += columns
}
// if DIRECTION is NORTH and ORIGIN is NOT in first row
else if (direction[0] === "N" && origin / columns >= 1) {
delta -= columns
}
return delta;
}
}