emotrend

Untitled

Jan 28th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function solve() {
  2.  
  3. let inputArr = JSON.parse(document.getElementById('arr').value);
  4.  
  5. let resultDiv = document.getElementById('result');
  6.  
  7.  
  8. let bulgarianArmyRegex = /^(BA) [0-9]{3} [0-9]{3}$/gm;
  9. let civilProtectionRegex = /^(CP) [0-9]{2} [0-9]{3}$/gm;
  10. let diplomaticRegex = /^(C|CT) [0-9]{4}$/gm;
  11. let foreignersRegex = /^(XX) [0-9]{4}$/gm;
  12. let transientRegex = /^[0-9]{3} [a-zA-z] [0-9]{3}$/gm;
  13. let sofiaRegex = /^(C|CA|CB) [0-9]{4} [a-zA-z]{1,2}$/gm;
  14. let provinceRegex = /^[a-zA-z]{1,2} [0-9]{4} [a-zA-Z]{1,2}$/gm;
  15.  
  16.  
  17. let resultObj = {
  18. 'BulgarianArmy': 0,
  19. 'CivilProtection': 0,
  20. 'Diplomatic': 0,
  21. 'Foreigners': 0,
  22. 'Transient': 0,
  23. 'Sofia': 0,
  24. 'Province': 0,
  25. 'Other': 0
  26. };
  27.  
  28. inputArr.forEach(el => {
  29. if (el.match(bulgarianArmyRegex)) {
  30. resultObj['BulgarianArmy']++;
  31. } else if (el.match(civilProtectionRegex)) {
  32. resultObj['CivilProtection']++;
  33. } else if (el.match(diplomaticRegex)) {
  34. resultObj['Diplomatic']++;
  35. } else if (el.match(foreignersRegex)) {
  36. resultObj['Foreigners']++;
  37. } else if (el.match(transientRegex)) {
  38. resultObj['Transient']++;
  39. } else if (el.match(sofiaRegex)) {
  40. resultObj['Sofia']++;
  41. } else if (el.match(provinceRegex)) {
  42. resultObj['Province']++;
  43. } else {
  44. resultObj['Other']++;
  45. }
  46. });
  47.  
  48.  
  49. let sorted = Object.entries(resultObj).sort((a, b) => {
  50. return b[1] - a[1] || a[0].localeCompare(b[0])
  51. });
  52.  
  53.  
  54. for (let el of sorted) {
  55. let tempP = document.createElement('p');
  56.  
  57. tempP.textContent = `${el[0]} -> ${el[1]}`;
  58.  
  59. resultDiv.appendChild(tempP);
  60.  
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment