Advertisement
kstoyanov

03. Lost js exam v2

Aug 3rd, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(keyword, text) {
  2.   const pat = /(north|east)\D*(\d{2})[^,]*(,)\D*(\d{6})/gi;
  3.   const msgPat = new RegExp(`(${keyword})(.*?)(${keyword})`, 'g');
  4.  
  5.  
  6.   const dirExtract = [...text.matchAll(pat)];
  7.   const msgExtact = msgPat.exec(text);  
  8.  
  9.   let latOutput = '';
  10.   let longOutput = '';
  11.  
  12.   dirExtract.forEach((el) => {
  13.     const [, arg1, arg2,, arg3] = el;
  14.  
  15.     const direction = arg1.toLowerCase();
  16.     const cords = `${arg2}.${arg3}`;
  17.  
  18.     if (direction === 'north') {
  19.       latOutput = `${cords} N`;
  20.     } else {
  21.       longOutput = `${cords} E`;
  22.     }
  23.   });
  24.  
  25.   console.log(latOutput);
  26.   console.log(longOutput);
  27.   console.log(`Message: ${msgExtact[2]}`);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement