VladoG

[JScore-JS-Fundamentals-Exercises] - 10. Orbit

Oct 6th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function orbit(input) {
  2.     let [rowM,colM] = input[0].split(' ');
  3.     let [sR,sC] = input[1].split(' ');
  4.     rowM = Number(rowM);
  5.     colM = Number(colM);
  6.     sR = Number(sR);
  7.     sC = Number(sC);
  8.     let orbit = 1;
  9.  
  10.     // I - Define MATRIX => matrix[row][col]   
  11.     //let matrix = Array(rowM).fill(Array(colM), undefined, rowM);
  12.    
  13.     // II - Define MATRIX => matrix[row][col]
  14.     let matrix = [];
  15.     for(var i=0; i<rowM; i++) {
  16.         matrix[i] = [];
  17.         for(var j=0; j<colM; j++) {
  18.             matrix[i][j] = undefined;
  19.         }
  20.     }
  21.  
  22.     // The Star's cell
  23.     matrix[sR][sC]=orbit;
  24.  
  25.     let startRow = sR - 1, startCol = sC - 1;
  26.     let endRow = sR + 1, endCol = sC + 1;
  27.  
  28.     while (true) {
  29.         orbit++;
  30.  
  31.         for (let row = startRow; row <=endRow; row++) {
  32.             for (let col = startCol; col<=endCol; col++){
  33.                 if (!((row==sR && col==sC) || row<0 || row>=rowM || col<0 || col>=colM)){
  34.                     if (row==startRow || row==endRow) {
  35.                         matrix[row][col] = orbit;
  36.                     } else {
  37.                         if (col==startCol || col==endCol){
  38.                             matrix[row][col] = orbit;
  39.                         }
  40.                     }
  41.  
  42.                 }
  43.  
  44.             }
  45.         }
  46.         startRow--;
  47.         endRow++;
  48.         startCol--;
  49.         endCol++;
  50.  
  51.         if (startRow<0 && endRow==rowM && startCol<0 && endCol==colM) {
  52.             break;
  53.         }
  54.     } // End while
  55.  
  56.     for (let row of matrix){
  57.         console.log(row.join(' '));
  58.     }
  59. }
Add Comment
Please, Sign In to add comment