Advertisement
viligen

orbit

May 24th, 2022
795
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function myFunc(arr) {
  2.     let [rows, cols, starRow, starCol] = arr;
  3.  
  4.     let field = [];
  5.     for (let i = 0; i < rows; i++) {
  6.         field[i] = Array(cols).fill();
  7.     }
  8.     field[starRow][starCol] = 1;
  9.     let currentNum = 1;
  10.  
  11.     while (currentNum < rows) {
  12.         let i = starRow - currentNum < 0 ? 0 : starRow - currentNum;
  13.         let j = starCol - currentNum < 0 ? 0 : starCol - currentNum;
  14.         let endRow =
  15.             starRow + currentNum >= rows ? rows : starRow + currentNum + 1;
  16.         let endCol =
  17.             starCol + currentNum >= cols ? cols : starCol + currentNum + 1;
  18.         for (i; i < endRow; i++) {
  19.             let j = starCol - currentNum < 0 ? 0 : starCol - currentNum;
  20.             for (j; j < endCol; j++) {
  21.                 if (field[i][j]) {
  22.                     continue;
  23.                 }
  24.                 field[i][j] = currentNum + 1;
  25.             }
  26.         }
  27.         currentNum++;
  28.     }
  29.  
  30.     for (let i = 0; i < rows; i++) {
  31.         console.log(field[i].join(" "));
  32.     }
  33. }
  34.  
  35. myFunc([6, 7, 2, 2]);
  36.  
Advertisement
RAW Paste Data Copied
Advertisement