Hello all!
Can someone help with this matrix? it needs to spiral inwards, from bottom right corner, clockwise...
I found this code... and it runs nicely but I need to adapt it in some ways:
- I should be able to provide number of rows and columns
- the grid should start from number 1
- it should start from bottom right corner, clockwise...
I will put the code that works, but not the way I need... can someone at least decipher it for me so I could try to do it??
Thanks
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
spiralArray = function (edge) {
var arr = Array(edge),
x = 0, y = edge,
total = edge * edge--,
dx = 1, dy = 0,
i = 0, j = 0;
while (y) arr[--y] = [];
while (i < total) {
arr[y][x] = i++;
x += dx; y += dy;
if (++j == edge) {
if (dy < 0) {x++; y++; edge -= 2}
j = dx; dx = -dy; dy = j; j = 0;
}
}
return arr;
}
// T E S T:
arr = spiralArray(edge = 3);
for (y= 0; y < edge; y++) console.log(arr[y].join(" "));
alert(arr.join('\n'))
</script>
</body>
</html>