Advertisement
Guest User

Untitled

a guest
May 13th, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. function catalogue(arr) {
  2. let obj = {};
  3.  
  4. for (let line of arr) {
  5. let [productName, price] = line.split(' : ');
  6. obj[productName] = price;
  7. }
  8.  
  9. let sortedItem = Object.entries(obj)
  10. .sort((a, b) => a[0].localeCompare(b[0]));
  11.  
  12. print(sortedItem);
  13.  
  14. function print(arr) {
  15. let lastName = '';
  16. for (let line of arr) {
  17. let name = line[0];
  18. let firstChar = name.charAt(0);
  19.  
  20. if (firstChar !== lastName[0]) {
  21. console.log(`${firstChar}`);
  22. }
  23. console.log(` ${name}: ${line[1]}`);
  24. lastName = name;
  25. }
  26. }
  27. }
  28.  
  29. catalogue([
  30. 'Appricot : 20.4',
  31. 'Fridge : 1500',
  32. 'TV : 1499',
  33. 'Deodorant : 10',
  34. 'Boiler : 300',
  35. 'Apple : 1.25',
  36. 'Anti-Bug Spray : 15',
  37. 'T-Shirt : 10'
  38. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement