Advertisement
braveheart1989

storeCatalogue

Oct 12th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function storeCatalogue (productInput) {
  2.     //Without nested Map();
  3.    
  4.     // let dataProduct = new Map ();
  5.     // for (let product of productInput) {
  6.     //  let [productName,productPrice] = product.split (" : ");
  7.     //  productPrice = Number (productPrice);
  8.     //  dataProduct.set (productName, productPrice);
  9.     // }
  10.     // let sorted = Array.from (dataProduct).sort ();
  11.     // let firstLetter = [];
  12.     // for (let [key,value] of sorted) {
  13.     //  firstLetter.push (key[0]);
  14.     // }
  15.     // let unique = firstLetter.filter ((v, i, a) => a.indexOf (v) === i);
  16.     // for (let i = 0; i < unique.length; i++) {
  17.     //  console.log (unique[i]);
  18.     //  for (let [key,value] of sorted) {
  19.     //      if (unique[i] == key[0]) {
  20.     //          console.log (`  ${key}: ${value}`);
  21.     //      }
  22.     //  }
  23.     // }
  24.    
  25.     //With Nested Map();
  26.    
  27.     let initials = new Map ();
  28.  
  29.     for (let initial of productInput) {
  30.         let product = initial.split (" : ");
  31.         let productName = product[0];
  32.         let productPrice = Number (product[1]);
  33.         let init = productName[0];
  34.         if (!initials.has (init)) {
  35.             initials.set (init, new Map ());
  36.         }
  37.         if (!initials.get (init).has (productName)) {
  38.             initials.get (init).set (productName, productPrice);
  39.         }
  40.     }
  41.  
  42.     function alphabeticallyCompare (a, b) {
  43.         return a[0].toLowerCase().localeCompare (b[0].toLowerCase());
  44.  
  45.     }
  46.     let sorted = Array.from (initials).sort (alphabeticallyCompare);
  47.  
  48.     for (let [key,value] of sorted) {
  49.         console.log (key);
  50.  
  51.         let productData = Array.from(value).sort(alphabeticallyCompare);
  52.         for (let [k,v] of productData) {
  53.             console.log (`  ${k}: ${v}`);
  54.         }
  55.     }
  56. }
  57. storeCatalogue (
  58.     [
  59.         "Appricot : 20.4",
  60.         "Fridge : 1500",
  61.         "TV : 1499",
  62.         "Deodorant : 10",
  63.         "Boiler : 300",
  64.         "Apple : 1.25",
  65.         "Anti-Bug Spray : 15",
  66.         "T-Shirt : 10"
  67.     ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement