This is a function of bypassing the two-dimensional array (matrix) in a spiral clockwise: (demo)
entryArray = [
[ 1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]
]
def f(a)
a.empty? ? [] : a.shift+f(a.transpose.reverse)
end
f(entryArray)
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
I tried to make an analog in JavaScript:
function transpose(a) {
return a.length === 0 ? a : a[0].map((col, i) => a.map((row) => row[i]))
}
function f(a) {
return a.length === 0 ? [] : a.shift + f(transpose(a).reverse());
}
f([[1, 2, 3, 4], [12, 13, 14, 5], [11, 16, 15, 6], [10, 9, 8, 7]]);
but it did not work, an error appears in the console:

Please tell me what the problem is, and is it possible to do it on JS?