Advertisement
kstoyanov

09. Catalogue js fundamentals v2

Jul 3rd, 2020
81
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.     const dub = listKeys.filter((el) => el.key === key).length;
  12.     if (dub === 0) {
  13.       listKeys.push(alphaObj);
  14.     }
  15.  
  16.     listKeys.forEach((el) => {
  17.       if (el.key === key) {
  18.         el.value.push(element);
  19.       }
  20.     });
  21.   });
  22.  
  23.   listKeys.forEach((element) => {
  24.     const productName = element.key;
  25.     const productPrice = element.value;
  26.     console.log(`${productName}`);
  27.     productPrice.forEach((el) => {
  28.       const [name, price] = el.split(':');
  29.       name.trimEnd();
  30.  
  31.  
  32.       console.log(`  ${name.trimEnd()}:${price}`);
  33.     });
  34.   });
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement