Advertisement
kstoyanov

09. Catalogue js fundamentals

Jul 3rd, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function catalogue(args) {
  2.   const listKeys = [];
  3.  
  4.   args.sort((a, b) => a.localeCompare(b));
  5.  
  6.   args.forEach((element) => {
  7.     const key = element.charAt(0);
  8.     const alphaObj = {};
  9.     alphaObj.key = key;
  10.     alphaObj.value = [];
  11.     listKeys.push(alphaObj);
  12.   });
  13.  
  14.   const filteredArr = listKeys.reduce((acc, current) => {
  15.     const x = acc.find((item) => item.key === current.key);
  16.     if (!x) {
  17.       return acc.concat([current]);
  18.     }
  19.     return acc;
  20.   }, []);
  21.  
  22.   args.forEach((element) => {
  23.     const key = element.charAt(0);
  24.  
  25.     filteredArr.forEach((el) => {
  26.       if (el.key === key) {
  27.         el.value.push(element);
  28.       }
  29.     });
  30.   });
  31.  
  32.   filteredArr.forEach((element) => {
  33.     const productName = element.key;
  34.     const productPrice = element.value;
  35.     console.log(`${productName}`);
  36.     productPrice.forEach((el) => {
  37.       const [name, price] = el.split(':');
  38.       name.trimEnd();
  39.  
  40.  
  41.       console.log(`  ${name.trimEnd()}:${price}`);
  42.     });
  43.   });
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement