Advertisement
dunyto

Untitled

Mar 2nd, 2022
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function catalogue(input) {
  2.     let catalog = [];
  3.        
  4.     for (let i = 0; i < input.length; i ++) {
  5.         let currItem = input[i].split(" : ");
  6.         let newItem = {};
  7.         newItem.initial = currItem[0][0];
  8.         newItem.productName =  currItem[0];
  9.         newItem.productPrice = currItem[1];
  10.         catalog.push(newItem);
  11.     }
  12.    
  13.     catalog.sort((a, b) => {
  14.         if (a.productName > b.productName) {
  15.             return 1
  16.         } else {
  17.             return -1
  18.         }
  19.     });
  20.  
  21.     let initList = [];
  22.     for (let item in catalog) {
  23.         if (initList.includes(catalog[item].initial)) {
  24.             console.log(`  ${catalog[item].productName}: ${catalog[item].productPrice}`);
  25.         } else {
  26.             console.log(`${catalog[item].initial}`);
  27.             console.log(`  ${catalog[item].productName}: ${catalog[item].productPrice}`);
  28.             initList.push(catalog[item].initial);
  29.         }
  30.        
  31.     }
  32.  
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement