{"size": 1145, "ext": "js", "max_stars_repo_path": "Contributors/BryanYunis/solutions/clockwiseSpiral.js", "max_stars_repo_name": "FergusDevelopmentLLC/Coders-Workshop", "max_stars_repo_stars_event_min_datetime": "2019-12-02T23:29:47.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T02:40:36.000Z", "max_issues_repo_path": "Contributors/BryanYunis/solutions/clockwiseSpiral.js", "max_issues_repo_name": "FergusDevelopmentLLC/Coders-Workshop", "max_issues_repo_issues_event_min_datetime": "2020-01-15T19:28:12.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-26T05:13:29.000Z", "max_forks_repo_path": "Contributors/BryanYunis/solutions/clockwiseSpiral.js", "max_forks_repo_name": "FergusDevelopmentLLC/Coders-Workshop", "max_forks_repo_forks_event_min_datetime": "2019-12-02T23:29:53.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-03T01:11:37.000Z"}
{"max_stars_count": 33, "max_issues_count": 39, "max_forks_count": 49, "avg_line_length": 18.4677419355, "max_line_length": 98, "alphanum_fraction": 0.4043668122}
const assert = require('assert');

const clockwiseSpiral = A => {
  const N = A[0].length,
    M = A.length
  let x = 0
  let y = 0
  let loop = 0
  const res = []
  let keepGoing = true

  while (keepGoing) {
    while (x < N - loop) {
      res.push(A[y][x++])
      if (res.length === M * N) {
        keepGoing = false
        break
      }
    }
    x--
    y++
    while (y < M - loop) {
      res.push(A[y++][x])
      if (res.length === M * N) {
        keepGoing = false
        break
      }
    }
    y--
    x--
    while (x >= loop) {
      res.push(A[y][x--])
      if (res.length === M * N) {
        keepGoing = false
        break
      }
    }
    x++
    y--
    loop++
    while (y >= loop) {
      res.push(A[y--][x])
      if (res.length === M * N) {
        keepGoing = false
        break
      }
    }
    y++
    x++
  }
  return res
}

const result = clockwiseSpiral([
  [1, 2, 3, 4, 5],
  [6, 7, 8, 9, 10],
  [11, 12, 13, 14, 15],
  [16, 17, 18, 19, 20]
])

assert.deepEqual(result, [1, 2, 3, 4, 5, 10, 15, 20, 19, 18, 17, 16, 11, 6, 7, 8, 9, 14, 13, 12])
