Sha256: c5cf35c204f986198825b9d5514d6ec87f41e8102dfa12d94bfbc5f537c0eeec

Contents?: true

Size: 747 Bytes

Versions: 117

Compression:

Stored size: 747 Bytes

Contents

class SpiralMatrix {

  static ofSize(size) {
    const spiral = Array(size).fill().map(() => Array(0));

    const totalNumbers = size**2;
    let currentNumber = 1;
    let topLeft = 0;
    let bottomRight = size - 1;

    while (currentNumber <= totalNumbers) {
      for (let x = topLeft; x <= bottomRight; x++) spiral[topLeft][x] = currentNumber++;
      for (let y = topLeft + 1; y <= bottomRight; y++) spiral[y][bottomRight] = currentNumber++;
      for (let x = bottomRight - 1; x >= topLeft; x--) spiral[bottomRight][x] = currentNumber++;
      for (let y = bottomRight - 1; y >= topLeft + 1; y--) spiral[y][topLeft] = currentNumber++;

      topLeft++;
      bottomRight--;
    }

    return spiral;
  }

}

export default SpiralMatrix;

Version data entries

117 entries across 117 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/spiral-matrix/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/spiral-matrix/example.js