ErolKZ

Untitled

Jan 1st, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. function solve(input) {
  2.  
  3. function objectCreation(input) {
  4.  
  5. let comments = {
  6.  
  7. articles: [],
  8.  
  9. users: [],
  10.  
  11. };
  12.  
  13. let pattern = /article \w+|user \w+/g;
  14.  
  15. while (input[0] !== undefined) {
  16.  
  17. let cur = input.shift();
  18.  
  19. if (cur.match(pattern) !== null) {
  20.  
  21. cur = cur.split(' ');
  22.  
  23. if (cur[0] === 'article') {
  24.  
  25. comments.articles.push(cur[1]);
  26.  
  27. } else if (cur[0] === 'user') {
  28.  
  29. comments.users.push(cur[1]);
  30.  
  31. }
  32.  
  33. } else {
  34.  
  35. cur = cur.split(/\sposts\son\s|:\s/g);
  36.  
  37. // console.log(cur);
  38.  
  39. if (comments.articles.includes(cur[1]) && comments.users.includes(cur[0])) {
  40.  
  41. if (comments.hasOwnProperty(cur[1])) {
  42.  
  43. comments[cur[1]][cur[0]] = cur[2];
  44.  
  45. } else {
  46.  
  47. comments[cur[1]] = { [cur[0]]: cur[2] };
  48.  
  49. }
  50.  
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. delete comments.articles;
  58.  
  59. delete comments.users;
  60.  
  61. return comments;
  62.  
  63. }
  64.  
  65.  
  66. function arrayCreation() {
  67.  
  68. let comments = objectCreation(input);
  69.  
  70. let arr = [];
  71.  
  72. for (let art in comments) {
  73.  
  74. let arr2 = [];
  75.  
  76. for (let key in comments[art]) {
  77.  
  78. arr2.push([key, comments[art][key]]);
  79.  
  80. }
  81.  
  82. arr.push([art, arr2]);
  83.  
  84. }
  85.  
  86. return arr;
  87.  
  88. }
  89.  
  90.  
  91. function sorting() {
  92.  
  93. let arr = arrayCreation();
  94.  
  95. arr.sort((a, b) => Object.keys(b[1]).length - Object.keys(a[1]).length);
  96.  
  97. for (let el of arr) {
  98.  
  99. el[1].sort((a, b) => a[0].localeCompare(b[0]));
  100.  
  101. }
  102.  
  103. return arr;
  104.  
  105. }
  106.  
  107.  
  108. function printing() {
  109.  
  110. let arr = sorting();
  111.  
  112. for (let el of arr) {
  113.  
  114. console.log(`Comments on ${el[0]}`);
  115.  
  116. for (let el2 of el[1]) {
  117.  
  118. let el3 = el2[1].split(', ');
  119.  
  120. console.log(`--- From user ${el2[0]}: ${el3[0]} - ${el3[1]}`);
  121.  
  122. }
  123.  
  124. }
  125.  
  126. }
  127.  
  128. printing();
  129.  
  130.  
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment