Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- function main(matrix, row) {
- //Create an empty matrix
- let spiralArray = createArray(matrix, row);
- let top = 0;
- let bottom = matrix - 1;
- let left = 0;
- let right = row - 1;
- let number = 0;
- while (top <= bottom && left <= right) {
- for (let i = left; i <= right; i++) {
- spiralArray[top][i] = ++number;
- }
- top++;
- for (let j = top; j <= bottom; j++) {
- spiralArray[j][right] = ++number;
- }
- right--;
- for (let k = right; k >= left; k--) {
- spiralArray[bottom][k] = ++number;
- }
- bottom--;
- for (let l = bottom; l >= top; l--) {
- spiralArray[l][left] = ++number;
- }
- left++;
- }
- return spiralArray;
- //Declaration
- function createArray(matrix, row) {
- let array = [];
- for (let i = 0; i < matrix; i++) {
- let subArray = [];
- subArray.length = row;
- array.push(subArray);
- }
- return array;
- }
- }
- let result = main(5, 5);
- console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement