v1
- by nikkypizza 9/14/202000
Setup HTML - click to add setup HTML
Setup JS - click to add setup JavaScript
delete caserun single casemove downdrag and drop case


ready



const buildMatrix = (inputNumber) => {
  let circularCounter = 1;
  let matrixSideLength = inputNumber;

  // Строим шаблон матрицы
  const matrixMap = [];
  for (let i = 0; i <= inputNumber - 1; i++) {
    matrixMap.push([]);
  };

  // Заполняем каждую сторону
  for (let i = 0; i < inputNumber; i++) {
    // Верх
    for (let j = 0; j < matrixSideLength; j++) {
      matrixMap[i][i + j] = circularCounter++;
    }
    // Правая сторона
    for (let j = 1; j < matrixSideLength; j++) {
      matrixMap[i + j][inputNumber - 1 - i] = circularCounter++;
    }
    // Нижняя сторона
    for (let j = matrixSideLength - 2; j > -1; j--) {
      matrixMap[inputNumber - 1 - i][i + j] = circularCounter++;
    }
    // Левая сторона
    for (let j = matrixSideLength - 2; j > 0; j--) {
      matrixMap[i + j][i] = circularCounter++;
    }
    
    // Сокращаем шаг стороны и повторяем пока не заполним матрицу полностью
    matrixSideLength -= 2;
  };
  return matrixMap;
};

buildMatrix(50)
delete caserun single casemove upmove downdrag and drop case


ready



const buildMatrix = (n) => {
  const result = Array.from({length: n}, () => []);
  let count = 1;
  let firstCol = 0;
  let firstRow = 0;
  let lastCol = n - 1;
  let lastRow = n - 1;

  const fillArr = () => {
    if ((firstCol <= lastCol) && (firstRow <= lastRow)) {
      for (let i = firstCol; i <= lastCol; i++) {
        result[firstRow][i] = count;
        count++;
      }
      firstRow++;

      for (let i = firstRow; i <= lastRow; i++) {
        result[i][lastCol] = count;
        count++;
      }
      lastCol--;

      for (let i = lastCol; i >= firstCol; i--) {
        result[lastRow][i] = count;
        count++;
      }
      lastRow--;

      for (let i = lastRow; i >= firstRow; i--) {
        result[i][firstCol] = count;
        count++;
      }
      firstCol++;

      fillArr();
    }
  }

  fillArr();

  return result;
};

buildMatrix(50)
delete caserun single casemove upmove downdrag and drop case


ready



function buildMatrix(n) {
  let arr = [];
  let count = 1;
  let circle = 0;
  let i = 0;
  let j = 0;

  for (let i = 0; i < n; i++) {
    arr[i] = [];
    arr[i].length = n;
  }

  for (circle; circle < n; circle++) {
    for (j = circle; j < arr.length - circle; j++) {
      arr[i][j] = count;
      count++;
    }
  
    for (++i; i < arr.length - circle; i++) {
      arr[i][j - 1] = count;
      count++;
    }
  
    for (--j; j > circle; j--) {
      arr[i - 1][j - 1] = count;
      count++;
    }
  
    for (--i; i > circle + 1; i--) {
      arr[i - 1][j] = count;
      count++;
    }
  }

  return arr;
}

buildMatrix(50)
delete caserun single casemove upmove downdrag and drop case


ready



function buildMatrix(n) {
  const getCurrentIndex = function* (){
    let index = 1;
    while(true) yield index++;
  };
  const results = Array.from({length: n}, ()=>[]);
  const values = getCurrentIndex();
  const column = {start: 0, end: n - 1,};
  const row = {start: 0, end: n - 1,};

  while(column.start <= column.end && row.start <= row.end) {
    for (let i = column.start; i <= column.end; i++){
      results[row.start][i] = values.next().value;
    }
    ++row.start;
    for (let i = row.start; i <= row.end; i++) {
      results[i][column.end] = values.next().value;
    }
    --column.end;
    for (let i = column.end; i >= column.start; i--) {
      results[row.end][i] = values.next().value;
    }
    --row.end;
    for (let i = row.end; i >= row.start; i--) {
      results[i][column.start] = values.next().value;
    }
    ++column.start;
  }
  return results;
};

buildMatrix(50)
delete caserun single casemove updrag and drop case


ready



const buildMatrix = (n) => {
  const result = Array.from({length: n}, () => []);
  let count = 1;
  let firstCol = 0;
  let firstRow = 0;
  let lastCol = n - 1;
  let lastRow = n - 1;


  while ((firstCol <= lastCol) && (firstRow <= lastRow)) {
    for (let i = firstCol; i <= lastCol; i++) {
      result[firstRow][i] = count;
      count++;
    }
    firstRow++;

    for (let i = firstRow; i <= lastRow; i++) {
      result[i][lastCol] = count;
      count++;
    }
    lastCol--;

    for (let i = lastCol; i >= firstCol; i--) {
      result[lastRow][i] = count;
      count++;
    }
    lastRow--;

    for (let i = lastRow; i >= firstRow; i--) {
      result[i][firstCol] = count;
      count++;
    }
    firstCol++;
  }

  return result;
};

buildMatrix(50)
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN