Advertisement
elena1234

System Componets - very important for sort and objects JavaScript

Sep 1st, 2021 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. You will be given a register of systems with components and subcomponents. You need to build an ordered database of all the elements that have been given to you.
  2. The elements are registered in a very simple way. When you have processed all of the input data, you must print them in a specific order. For every System you must print its components in a specified order, and for every Component, you must print its Subcomponents in a specified order.
  3. The Systems you’ve stored must be ordered by amount of components, in descending order, as first criteria, and by alphabetical order as second criteria. The Components must be ordered by amount of Subcomponents, in descending order.
  4. Input
  5. The input comes as array of strings. Each element holds data about a system, a component in that system, and a subcomponent in that component. If the given system already exists, you should just add the new component to it. If even the component exists, you should just add the new subcomponent to it. The subcomponents will always be unique. The input format is:
  6. {systemName} | {componentName} | {subcomponentName}
  7. All of the elements are strings, and can contain any ASCII character. The string comparison for the alphabetical order is case-insensitive.
  8. Output
  9. As output you need to print all of the elements, ordered exactly in the way specified above. The format is:
  10. {systemName}
  11.  |||{componentName}
  12.  |||{component2Name}
  13.  ||||||{subcomponentName}
  14.  ||||||{subcomponent2Name}
  15.  {system2Name}
  16.  ...”
  17.  
  18. Examples
  19. Input  
  20. ['SULS | Main Site | Home Page',
  21. 'SULS | Main Site | Login Page',
  22. 'SULS | Main Site | Register Page',
  23. 'SULS | Judge Site | Login Page',
  24. 'SULS | Judge Site | Submittion Page',
  25. 'Lambda | CoreA | A23',
  26. 'SULS | Digital Site | Login Page',
  27. 'Lambda | CoreB | B24',
  28. 'Lambda | CoreA | A24',
  29. 'Lambda | CoreA | A25',
  30. 'Lambda | CoreC | C4',
  31. 'Indice | Session | Default Storage',
  32. 'Indice | Session | Default Security'] 
  33.  
  34. Output
  35. Lambda
  36. |||CoreA
  37. ||||||A23
  38. ||||||A24
  39. ||||||A25
  40. |||CoreB
  41. ||||||B24
  42. |||CoreC
  43. ||||||C4
  44. SULS
  45. |||Main Site
  46. ||||||Home Page
  47. ||||||Login Page
  48. ||||||Register Page
  49. |||Judge Site
  50. ||||||Login Page
  51. ||||||Submittion Page
  52. |||Digital Site
  53. ||||||Login Page
  54. Indice
  55. |||Session
  56. ||||||Default Storage
  57. ||||||Default Security
  58.  
  59.  
  60.  
  61.  
  62. function solve(array) {
  63.     let register = new {};
  64.     array.forEach(line => {
  65.         let [systemName, componentName, subcomponentName] = line.split(' | ');
  66.         if (!register[systemName]) {
  67.             register[systemName] = {};
  68.             register[systemName][componentName] = [];
  69.             register[systemName][componentName].push(subcomponentName);
  70.         } else if (register[systemName] && !register[systemName][componentName]) {
  71.             register[systemName][componentName] = [];
  72.             register[systemName][componentName].push(subcomponentName);
  73.         } else {
  74.             register[systemName][componentName].push(subcomponentName);
  75.         }
  76.     })
  77.  
  78.     // Order the systemsName in register by amount of components, in descending order, as first criteria, and by alphabetical order as second criteria
  79.      // as string [] only keys
  80.     let sortedSystemsName = Object.keys(register).sort((a, b) => {
  81.         if (Object.keys(register[b]).length > Object.keys(register[a]).length) {
  82.             return 1;
  83.         } else if (Object.keys(register[b]).length === Object.keys(register[a]).length) {
  84.             return a.localeCompare(b);
  85.         } else {
  86.             return -1;
  87.         }
  88.     });
  89.  
  90.     sortedSystemsName.forEach(currentSystemName => {
  91.         console.log(currentSystemName); // as string
  92.  
  93.         let sortedComponentsBySubcomponentsLength = Object.keys(register[currentSystemName]).sort((a, b) =>
  94.             Object.keys((register[currentSystemName][b]).length - Object.keys((register[currentSystemName][a].length))));
  95.  
  96.         sortedComponentsBySubcomponentsLength.forEach((currentComponent => {
  97.             console.log(`|||${currentComponent}`); // as string
  98.  
  99.             register[currentSystemName][currentComponent].forEach(subcomponent => console.log(`||||||${subcomponent}`));
  100.         }));
  101.     });
  102. }
  103.  
  104.  
  105. solve(['SULS | Main Site | Home Page',
  106. 'SULS | Main Site | Login Page',
  107. 'SULS | Main Site | Register Page',
  108. 'SULS | Judge Site | Login Page',
  109. 'SULS | Judge Site | Submittion Page',
  110. 'Lambda | CoreA | A23',
  111. 'SULS | Digital Site | Login Page',
  112. 'Lambda | CoreB | B24',
  113. 'Lambda | CoreA | A24',
  114. 'Lambda | CoreA | A25',
  115. 'Lambda | CoreC | C4',
  116. 'Indice | Session | Default Storage',
  117. 'Indice | Session | Default Security']
  118. )
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement