1function(A){
2 let retArray = [];
3 let T = 0;
4 let B = A.length - 1;
5 let L = 0;
6 let R = A[0].length - 1;
7 let d = 0;
8
9 while (T <= B && L <= R) {
10 if (d === 0) {
11 for (let i = L; i <= R; i++) {
12 retArray.push(A[T][i]);
13 }
14 T++;
15 d = 1;
16 } else if (d === 1) {
17 for (let i = T; i <= B; i++) {
18 retArray.push(A[i][R]);
19 }
20 R--;
21 d = 2;
22 } else if (d === 2) {
23 for (let i = R; i >= L; i--) {
24 retArray.push(A[B][i]);
25 }
26 B--;
27 d = 3;
28 } else if (d === 3) {
29 for (let i = B; i >= T; i--) {
30 retArray.push(A[i][L]);
31 }
32 L++;
33 d = 0;
34 }
35 }
36
37 return retArray;
38 }