emotrend

2ra

Feb 7th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. function solve() {
  2.  
  3. let keyword = document.getElementById('str').value;
  4. let inputStr = document.getElementById('text').value;
  5. let htmlResult = document.getElementById('result');
  6.  
  7. let pattern = /(north|east).*?([0-9]{2})[^,]*?\,[^,]*?([0-9]{6})/gmi;
  8.  
  9. let messagePattern = new RegExp(`(?<=${keyword}).*(?=${keyword})`);
  10.  
  11. let dirPattern = /north|east/gmi;
  12.  
  13. let secretMessage = inputStr.match(messagePattern)[0];
  14.  
  15. let result = inputStr.match(pattern);
  16.  
  17. let m;
  18.  
  19. let eastCounter;
  20. let northCounter;
  21.  
  22. let resultArr = [];
  23. let matchesArr = [];
  24.  
  25. while ((m = pattern.exec(inputStr)) !== null) {
  26.  
  27. m.forEach((match, groupIndex) => {
  28.  
  29. if (groupIndex === 0) {
  30. let tempMatches = match.match(dirPattern);
  31. resultArr.push(`${match}`);
  32. if (tempMatches[0].toLowerCase() === 'east') {
  33. eastCounter = resultArr.length - 1;
  34. } else if(tempMatches[0].toLowerCase() === 'north') {
  35. northCounter = resultArr.length - 1;
  36. }
  37. }
  38. matchesArr.push(match);
  39.  
  40. // let tempMatch = match.split(' ');
  41. // console.log(tempMatch);
  42.  
  43. // if (tempMatch.length < 2) {
  44. // matchesArr.push(match);
  45. // }
  46.  
  47. });
  48. }
  49. let eastCoord = `${matchesArr[(eastCounter * 4) + 2]}.${matchesArr[(eastCounter * 4) + 3]} E`;
  50. let northCoord = `${matchesArr[(northCounter * 4) + 2]}.${matchesArr[(northCounter * 4) + 3]} N`;
  51.  
  52. let msg = `Message: ${secretMessage}`;
  53.  
  54. let secondP = document.createElement('p');
  55. secondP.textContent = northCoord;
  56. htmlResult.appendChild(secondP);
  57.  
  58. let tempP = document.createElement('p');
  59. tempP.textContent = eastCoord;
  60. htmlResult.appendChild(tempP);
  61.  
  62. let thirdP = document.createElement('p');
  63. thirdP.textContent = msg;
  64. htmlResult.appendChild(thirdP);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment