Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The navigation field is a 2D grid. You are given the names of 3 star systems, along with their coordinates
- (sx, sy) and the Normandy’s initial coordinates(nx, ny). Assume that a star’s coordinates are in the center of
- a 2x2 rectangle. The Normandy always moves in an upwards direction, 1 unit every turn. Your task is to inform
- the Normandy of its current location during its movement. The Normandy can only be at one location at a time.
- The possible locations are "<star1 name>", "<star2 name>", "<star3 name>" and "space". In other words,
- if the Normandy is in the range of Alpha-Centauri, her location is "alpha-centauri". If she's not in the range
- of any star system, her location is just "space". Star systems will NOT overlap.
- The input is passed as array of several arguments: • The first arguments will contain each star system's name
- and coordinates in the format "<name> <x> <y>", separated by spaces. The name will be a single word, without spaces.
- The fourth argument will contain the Normandy’s initial coordinates in the format "<x> <y>", separated by spaces.
- The fifth, last argument, will contain the number n – the number of turns the Normandy will be moving.
- The output consists of n + 1 lines – the Normandy’s initial location, plus the locations she was in during
- her movement, each on a separate line. All locations must be printed lowercase. */
- "use strict";
- function findNormandyLocation(arr) {
- var starSystems = [];
- for (var i = 0; i < 3; i += 1) {
- var currentSystem = args[i].toLowerCase().split(/\s+/).filter(Boolean);
- var starArr = [ currentSystem[0], Number(currentSystem[1]), Number(currentSystem[2]) ];
- starSystems.push(starArr);
- }
- var spaceshipStartLocation = args[3].split(/\s+/).filter(Boolean);
- var shipX = Number(spaceshipStartLocation[0]);
- var shipY = Number(spaceshipStartLocation[1]);
- var movesN = Number(args[4]);
- for (i = 0; i <= movesN; i += 1) {
- console.log(checkSpaceshipIsInTheRangeStarSystem(shipX, shipY, starSystems));
- shipY += 1;
- }
- function checkSpaceshipIsInTheRangeStarSystem(x, y, stars) {
- for (var next in stars) {
- if (stars.hasOwnProperty(next)) {
- var starMinX = Number(stars[next][1]) - 1;
- var starMaxX = Number(stars[next][1]) + 1;
- var starMinY = Number(stars[next][2]) - 1;
- var starMaxY = Number(stars[next][2]) + 1;
- if (x >= starMinX && x <= starMaxX && y >= starMinY && y <= starMaxY) {
- return stars[next][0];
- }
- }
- }
- return 'space';
- }
- }
- findNormandyLocation([
- 'Sirius 3 7',
- 'Alpha-Centauri 7 5',
- 'Gamma-Cygni 10 10',
- '8 1',
- '6'
- ]);
- findNormandyLocation([
- 'Terra-Nova 16 2',
- 'Perseus 2.6 4.8',
- 'Virgo 1.6 7',
- '2 5',
- '4'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement