Advertisement
VeselaVideva

Arrays and Nested Arrays - Exercise - 12. Orbit

Feb 10th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function orbit(arr) {
  2.     let rows = arr[0];
  3.     let cols = arr[1];
  4.     let starRow = arr[2];
  5.     let starCol = arr[3];
  6.  
  7.     let matrix = [];
  8.     for(let i = 0; i < rows; i++) {
  9.         matrix.push([]);
  10.     }
  11.  
  12.     for(let row = 0; row < rows; row++) {
  13.         for(let col = 0; col < cols; col++) {
  14.             matrix[row][col] = Math.max(Math.abs(row - starRow), Math.abs(col - starCol)) + 1;
  15.         }
  16.     }
  17.  
  18.     console.log(matrix.map(row => row.join(" ")).join("\n"));
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement