Todorov_Stanimir

01. The Isle of Man Techn. Fund. Final Exam 14.04.19 II gr.

May 21st, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theIsleOfManTTRace(input) {
  2.     for (let i = 0; i < input.length; i++) {
  3.  
  4.         let nameOfRacer = '';
  5.         let lengthOfGeohashcode = 0;
  6.         let encryptedGeohashCode = '';
  7.         let geohashCode = '';
  8.  
  9.         let currentRacer = input[i];
  10.         let beginingFinishingName = 0;
  11.         let indexOflengthOfGeohashcode = 0;
  12.  
  13.         for (let i = 0; i < currentRacer.length; i++) {
  14.             if (currentRacer[i] === '#' ||
  15.                 currentRacer[i] === '$' ||
  16.                 currentRacer[i] === '%' ||
  17.                 currentRacer[i] === '*' ||
  18.                 currentRacer[i] === '&') {
  19.                 beginingFinishingName++;
  20.             }
  21.             if (beginingFinishingName === 2 && currentRacer[i + 1] === '=') {
  22.                 indexOflengthOfGeohashcode = i + 1;
  23.                 break;
  24.             }
  25.         }
  26.  
  27.         let indexOfEncryptedGeohashCode = 0;
  28.  
  29.         for (let i = currentRacer.length; i > indexOflengthOfGeohashcode; i--) {
  30.             if (currentRacer[i] === "!" && currentRacer[i - 1] === "!") {
  31.                 indexOfEncryptedGeohashCode = i;
  32.             }
  33.         }
  34.         let isCorrectMessage = true;
  35.         if (indexOflengthOfGeohashcode >= indexOfEncryptedGeohashCode) {
  36.             isCorrectMessage = false
  37.         } else {
  38.             nameOfRacer = currentRacer.substring(0, indexOflengthOfGeohashcode);
  39.             lengthOfGeohashcode = Number(currentRacer.substring(indexOflengthOfGeohashcode + 1, (indexOfEncryptedGeohashCode - 1)));
  40.             encryptedGeohashCode = currentRacer.substr(indexOfEncryptedGeohashCode + 1, currentRacer.length)
  41.         }
  42.         if (nameOfRacer[0] !== nameOfRacer[nameOfRacer.length - 1] ||
  43.             (nameOfRacer[0] !== '#' && nameOfRacer[0] !== '$' && nameOfRacer[0] !== '%' && nameOfRacer[0] !== '*' && nameOfRacer[0] !== '&') ||
  44.             encryptedGeohashCode.length !== Number(lengthOfGeohashcode)) {
  45.             isCorrectMessage = false;
  46.         } else {
  47.             nameOfRacer = nameOfRacer.substr(1, nameOfRacer.length - 2);
  48.             for (let o = 0; o < nameOfRacer.length; o++) {
  49.                 if (nameOfRacer.charCodeAt(o) < 65 || (nameOfRacer.charCodeAt(o) >90 && nameOfRacer.charCodeAt(o) < 97) || nameOfRacer.charCodeAt(o) > 122) {
  50.                     isCorrectMessage = false;
  51.                     break;
  52.                 }
  53.             }
  54.             for (let z = 0; z < encryptedGeohashCode.length; z++) {
  55.                 let currentPosition = String.fromCharCode((encryptedGeohashCode.charCodeAt(z) + lengthOfGeohashcode));
  56.                 geohashCode += currentPosition;
  57.             }
  58.         }
  59.  
  60.         if (isCorrectMessage === true) {
  61.             console.log(`Coordinates found! ${nameOfRacer} -> ${geohashCode}`);
  62.             break;
  63.         } else
  64.             console.log('Nothing found!');
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment