Advertisement
YavorJS

4. Store Catalogue

Jun 8th, 2017
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. function storeCatalogue(arr) {
  2. let products = new Map();
  3. for (let line of arr) {
  4. let data = line.split(/\b\s:\s\b/);
  5. let letter = line[0][0];
  6. if (!products.has(letter)) {
  7. products.set(letter, data);
  8. }
  9. else {
  10. products.set(letter,products.get(letter)+","+data);
  11. }
  12. }
  13. let myProducts = new Map([...products.entries()].sort());
  14. for (let [letter, items] of myProducts) {
  15. console.log(letter);
  16. if(items.constructor !== Array){
  17. items=items.split(',');
  18. }
  19. let products = [];
  20. for (let i = 0; i < items.length; i+=2) {
  21. let product =items[i];
  22. let price =items[i+1];
  23. let line = product+": "+price;
  24. products.push(line);
  25. products.sort();
  26. }
  27. for (let product of products) {
  28. console.log(` ${product}`);
  29. }
  30. }
  31. }
  32.  
  33. storeCatalogue([
  34. 'Appricot : 20.4',
  35. 'Fridge : 1500',
  36. 'TV : 1499',
  37. 'Deodorant : 10',
  38. 'Boiler : 300',
  39. 'Apple : 1.25',
  40. 'Anti-Bug Spray : 15',
  41. 'T-Shirt : 10'
  42. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement