Advertisement
ad2bg

treasureLocator

May 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function treasureLocator(input) {
  2.  
  3.     const pointInRectangle =
  4.         (x, y, xMin, xMax, yMin, yMax) =>
  5.             xMin <= x && x <= xMax && yMin <= y && y <= yMax;
  6.  
  7.     function locate(x, y) {
  8.         const islands = [
  9.             ['Tokelau', 8, 9, 0, 1],
  10.             ['Tuvalu', 1, 3, 1, 3],
  11.             ['Samoa', 5, 7, 3, 6],
  12.             ['Tonga', 0, 2, 6, 8],
  13.             ['Cook', 4, 9, 7, 8],
  14.         ];
  15.  
  16.         let location = 'On the bottom of the ocean';
  17.         for (const island of islands) {
  18.             if (pointInRectangle(x, y, island[1], island[2], island[3], island[4])) {
  19.                 location = island[0];
  20.             }
  21.         }
  22.         return location;
  23.     }
  24.  
  25.     for (let i = 0; i < input.length; i += 2) {
  26.         const x = input[i];
  27.         const y = input[i + 1];
  28.  
  29.         console.log(locate(x, y));
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement