-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.js
39 lines (34 loc) · 855 Bytes
/
03.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
function n(n){
return n > 9 ? "" + n: "0" + n;
}
spiralArray = function (edge) {
var array = Array(edge);
var location_x = 0;
var location_y = edge;
var total = edge * edge--;
var direction_x = 1;
var direction_y = 0;
var i = 0;
var j = 0;
while (location_y) {
array[--location_y] = [];
}
while (i < total) {
// value being assigned
array[location_y][location_x] = n(total - i++);
location_x += direction_x;
location_y += direction_y;
if (++j == edge) {
if (direction_y < 0) {
location_x++;
location_y++;
edge -= 2
}
j = direction_x;
direction_x = -direction_y;
direction_y = j;
j = 0;
}
}
return array;
}