# Spiral Traverse

![](https://3743232000-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHW2IQuh2PFpWJDvBz2FF%2Fuploads%2Fgit-blob-4ea420c864a206e929f037f303fd58cfeaab922c%2FScreenshot%202023-01-21%20at%2016.41.58.png?alt=media)

* NxM, 1

```jsx
function spiralTraverse(arr) {
  const answer = [];
  

  let startCol = 0;
  let endCol = arr[0].length - 1;

  let startRow = 0;
  let endRow = arr.length - 1;
  while (endCol - startCol >= 0 && endRow - startRow >= 0) {

    // right
    for (let col = startCol; col <= endCol; col++) {
      answer.push(arr[startRow][col]);
    }

    // down
    startRow++;
    for (let row = startRow; row <= endRow; row++) {
      answer.push(arr[row][endCol]);
    }

    // left
    endCol--;
    for (let col = endCol; col >= startCol; col--) {
      if (startRow > endRow) break;
      answer.push(arr[endRow][col]);
    }

    // up
    endRow--;
    for (let row = endRow; row >= startRow; row--) {
      if (startCol > endCol) break;
      answer.push(arr[row][startCol]);
    }

    startCol++;

  }

  return answer;
}

// Do not edit the line below.
exports.spiralTraverse = spiralTraverse;
```
