Advertisement
Todorov_Stanimir

04. Airport Check Exercise: String and RegExp

Oct 24th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     const inputElem = document.getElementById('string');
  3.     const resultElem = document.getElementById('result');
  4.     const patternName = /( ([A-Z]{1}[A-Za-z]*)-([A-Z]{1}[A-Za-z]*).-([A-Z]{1}[A-Za-z]*) )|( ([A-Z]{1}[A-Za-z]*)-([A-Z]{1}[A-Za-z]*) )/g;
  5.     const patternAirport = / [A-Z]{3}\/[A-Z]{3} /g;
  6.     const patternFly = / [A-Z]{1,3}[\d]{1,5} /g;
  7.     const patternCompany = /\- [A-Z]{1}[a-z]*[*]{1}[A-Z]{1}[a-z]* /g;
  8.     let [firstPart, secondPart] = inputElem.value.split(',');
  9.     secondPart = secondPart.trim()
  10.     let name = '';
  11.     let airport = '';
  12.     let fly = '';
  13.     let company = ''
  14.     if (firstPart.match(patternName)) {
  15.         name = firstPart.match(patternName)[0];
  16.         while (name.includes('-')) {
  17.             name = name.replace('-', ' ');
  18.         }
  19.         name = name.trim();
  20.     }
  21.     if (firstPart.match(patternAirport)) {
  22.         airport = firstPart.match(patternAirport)[0].trim();
  23.     }
  24.     if (firstPart.match(patternFly)) {
  25.         fly = firstPart.match(patternFly)[0].trim();
  26.     }
  27.     if (firstPart.match(patternCompany)) {
  28.         company = firstPart.match(patternCompany)[0].trim();
  29.         while (company.includes('- ')) {
  30.             company = company.replace('- ', '');
  31.         }
  32.         while (company.includes('*')) {
  33.             company = company.replace('*', ' ');
  34.         }
  35.         company = company.trim()
  36.     }
  37.  
  38.     let result = ''
  39.     if (secondPart === 'name') {
  40.         result = `Mr/Ms, ${name}, have a nice flight!`
  41.     } else if (secondPart === 'flight') {
  42.         result = `Your flight number ${fly} is from ${airport.split('/')[0]} to ${airport.split('/')[1]}.`
  43.     } else if (secondPart === 'company') {
  44.         result = `Have a nice flight with ${company}.`
  45.     } else if (secondPart === 'all') {
  46.         result = `Mr/Ms, ${name}, your flight number ${fly} is from ${airport.split('/')[0]} to ${airport.split('/')[1]}. Have a nice flight with ${company}.`
  47.     }
  48.     let newSpanElem = document.createElement('span');
  49.     newSpanElem.textContent = result;
  50.     resultElem.appendChild(newSpanElem);
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement