Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let keyword = document.getElementById('str').value;
- let inputStr = document.getElementById('text').value;
- let htmlResult = document.getElementById('result');
- let pattern = /(north|east).*?([0-9]{2})[^,]*?\,[^,]*?([0-9]{6})/gmi;
- let messagePattern = new RegExp(`(?<=${keyword}).*(?=${keyword})`);
- let dirPattern = /north|east/gmi;
- let secretMessage = inputStr.match(messagePattern)[0];
- let result = inputStr.match(pattern);
- let m;
- let eastCounter;
- let northCounter;
- let resultArr = [];
- let matchesArr = [];
- while ((m = pattern.exec(inputStr)) !== null) {
- m.forEach((match, groupIndex) => {
- if (groupIndex === 0) {
- let tempMatches = match.match(dirPattern);
- resultArr.push(`${match}`);
- if (tempMatches[0].toLowerCase() === 'east') {
- eastCounter = resultArr.length - 1;
- } else if(tempMatches[0].toLowerCase() === 'north') {
- northCounter = resultArr.length - 1;
- }
- }
- matchesArr.push(match);
- // let tempMatch = match.split(' ');
- // console.log(tempMatch);
- // if (tempMatch.length < 2) {
- // matchesArr.push(match);
- // }
- });
- }
- let eastCoord = `${matchesArr[(eastCounter * 4) + 2]}.${matchesArr[(eastCounter * 4) + 3]} E`;
- let northCoord = `${matchesArr[(northCounter * 4) + 2]}.${matchesArr[(northCounter * 4) + 3]} N`;
- let msg = `Message: ${secretMessage}`;
- let secondP = document.createElement('p');
- secondP.textContent = northCoord;
- htmlResult.appendChild(secondP);
- let tempP = document.createElement('p');
- tempP.textContent = eastCoord;
- htmlResult.appendChild(tempP);
- let thirdP = document.createElement('p');
- thirdP.textContent = msg;
- htmlResult.appendChild(thirdP);
- }
Advertisement
Add Comment
Please, Sign In to add comment