Advertisement
ShadowEmbrace

Expedition

Feb 6th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let str = document.getElementById('str').value;
  3.     let text = document.getElementById('text').value;
  4.     let result = document.getElementById('result');
  5.  
  6.     let regex = /(north|east)[\s\S]*?(\d{2})[^,]*?,[^,]*?(\d{6})/gmi;
  7.     let matches = directionMatches(regex, text);
  8.  
  9.     let coordinates = [
  10.         {
  11.             'direction': 'north',
  12.             'degrees': 0,
  13.             'minutes': 0
  14.         },
  15.         {
  16.             'direction': 'east',
  17.             'degrees': 0,
  18.             'minutes': 0
  19.         }
  20.     ];
  21.  
  22.     for (let i = 0; i < matches.length; i++) {
  23.         for (let coords of coordinates) {
  24.             if (matches[i][1].toLowerCase() === coords.direction) {
  25.                 coords.degrees = matches[i][2];
  26.                 coords.minutes = matches[i][3];
  27.             }
  28.         }
  29.     }
  30.  
  31.     let startOfMessage = text.indexOf(str) + str.length;
  32.     let endOfMessage = text.substr(startOfMessage).indexOf(str);
  33.     let message = text.substr(startOfMessage, endOfMessage);
  34.  
  35.     let pN = document.createElement('p');
  36.     pN.textContent = `${coordinates[0].degrees}.${coordinates[0].minutes} N`;
  37.  
  38.     let pE = document.createElement('p');
  39.     pE.textContent = `${coordinates[1].degrees}.${coordinates[1].minutes} E`;
  40.  
  41.     let pM = document.createElement('p');
  42.     pM.textContent = `Message: ${message}`;
  43.  
  44.     result.appendChild(pN);
  45.     result.appendChild(pE);
  46.     result.appendChild(pM);
  47.  
  48.  
  49.     function directionMatches(regex, text) {
  50.         let result = [];
  51.         while (1) {
  52.             let currMatch = regex.exec(text);
  53.  
  54.             if (currMatch === null) {
  55.                 break;
  56.             } else {
  57.                 result.push(currMatch);
  58.             }
  59.         }
  60.         return result;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement