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>

Dani AI

Generated

A compact, robust way to meet the three requests (variable rows/cols, 1-based numbering, start at bottom-right and spiral clockwise) is to iterate the matrix edge-by-edge with four boundary indices: top, bottom, left, right. For a clockwise spiral that begins at the bottom-right corner the traversal order is: move left across the bottom row, move up the left column, move right across the top row, then move down the right column — repeat while the boundaries remain valid. This clarifies the behavior that confused and directly answers 's goal.

function spiral(rows, cols) {
  const a = Array.from({length: rows}, () => Array(cols));
  let top = 0, bottom = rows - 1, left = 0, right = cols - 1;
  let n = 1;
  while (left <= right && top <= bottom) {
    for (let c = right; c >= left; c--) a[bottom][c] = n++;
    bottom--;
    if (top > bottom) break;
    for (let r = bottom; r >= top; r--) a[r][left] = n++;
    left++;
    if (left > right) break;
    for (let c = left; c <= right; c++) a[top][c] = n++;
    top++;
    if (top > bottom) break;
    for (let r = top; r <= bottom; r++) a[r][right] = n++;
    right--;
  }
  return a;
}

This function returns a 2D array; use console.log(matrix.map(r => r.join(' ')).join('\n')) or console.table to inspect results. Common pitfalls: mixing row/column indices, off-by-one boundary updates, and failing to check termination after shrinking a boundary (the example handles that by breaking when needed). The approach works for rectangular grids and naturally handles the center cell for odd-sized dimensions.

I'm totally confused what on earth you want this to do. When I ran it, it gave me an alert that said:

0,1,2
7,8,3
6,5,4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.