Advertisement
dimipan80

Exams - To The Stars!

Nov 22nd, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The navigation field is a 2D grid. You are given the names of 3 star systems, along with their coordinates
  2. (sx, sy) and the Normandy’s initial coordinates(nx, ny). Assume that a star’s coordinates are in the center of
  3. a 2x2 rectangle. The Normandy always moves in an upwards direction, 1 unit every turn. Your task is to inform
  4. the Normandy of its current location during its movement. The Normandy can only be at one location at a time.
  5. The possible locations are "<star1 name>", "<star2 name>", "<star3 name>" and "space". In other words,
  6. if the Normandy is in the range of Alpha-Centauri, her location is "alpha-centauri". If she's not in the range
  7. of any star system, her location is just "space". Star systems will NOT overlap.
  8. The input is passed as array of several arguments: •  The first arguments will contain each star system's name
  9. and coordinates in the format "<name> <x> <y>", separated by spaces. The name will be a single word, without spaces.
  10. The fourth argument will contain the Normandy’s initial coordinates in the format "<x> <y>", separated by spaces.
  11. The fifth, last argument, will contain the number n – the number of turns the Normandy will be moving.
  12. The output consists of n + 1 lines – the Normandy’s initial location, plus the locations she was in during
  13. her movement, each on a separate line. All locations must be printed lowercase. */
  14.  
  15. "use strict";
  16.  
  17. function findNormandyLocation(arr) {
  18.         var starSystems = [];
  19.     for (var i = 0; i < 3; i += 1) {
  20.         var currentSystem = args[i].toLowerCase().split(/\s+/).filter(Boolean);
  21.         var starArr = [ currentSystem[0], Number(currentSystem[1]), Number(currentSystem[2]) ];
  22.         starSystems.push(starArr);
  23.     }
  24.  
  25.     var spaceshipStartLocation = args[3].split(/\s+/).filter(Boolean);
  26.     var shipX = Number(spaceshipStartLocation[0]);
  27.     var shipY = Number(spaceshipStartLocation[1]);
  28.  
  29.     var movesN = Number(args[4]);
  30.  
  31.     for (i = 0; i <= movesN; i += 1) {
  32.         console.log(checkSpaceshipIsInTheRangeStarSystem(shipX, shipY, starSystems));
  33.         shipY += 1;
  34.     }
  35.  
  36.     function checkSpaceshipIsInTheRangeStarSystem(x, y, stars) {
  37.         for (var next in stars) {
  38.             if (stars.hasOwnProperty(next)) {
  39.                 var starMinX = Number(stars[next][1]) - 1;
  40.                 var starMaxX = Number(stars[next][1]) + 1;
  41.                 var starMinY = Number(stars[next][2]) - 1;
  42.                 var starMaxY = Number(stars[next][2]) + 1;
  43.  
  44.                 if (x >= starMinX && x <= starMaxX && y >= starMinY && y <= starMaxY) {
  45.                     return stars[next][0];
  46.                 }
  47.             }
  48.         }
  49.  
  50.         return 'space';
  51.     }
  52. }
  53.  
  54. findNormandyLocation([
  55.     'Sirius 3 7',
  56.     'Alpha-Centauri 7 5',
  57.     'Gamma-Cygni 10 10',
  58.     '8 1',
  59.     '6'
  60. ]);
  61.  
  62. findNormandyLocation([
  63.     'Terra-Nova 16 2',
  64.     'Perseus 2.6 4.8',
  65.     'Virgo 1.6 7',
  66.     '2 5',
  67.     '4'
  68. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement