Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. function solve(arr) {
  2.  
  3. let obj = {};
  4.  
  5. for (let i = 0; i < arr.length; i++) {
  6.  
  7. let line = arr[i].split(" -> ");
  8.  
  9. let city = line[0];
  10. let product = line[1];
  11. let price = line[2].split(" : ").reduce((a, b) => a * b);
  12.  
  13. if (typeof obj[city] === "undefined") {
  14. obj[city] = {};
  15. }
  16.  
  17. obj[city][product] = price;
  18.  
  19. }
  20.  
  21. for (let key in obj) {
  22. console.log(`Town - ${key}`);
  23. for (let value of obj[key]) {
  24. for (let objElementKeyKey in value) {
  25. console.log(`$$$${objElementKeyKey} : ${value[objElementKeyKey]}`);
  26. }
  27. }
  28. }
  29. }
  30. solve(['Sofia -> Laptops HP -> 200 : 2000',
  31. 'Sofia -> Raspberry -> 200000 : 1500',
  32. 'Sofia -> Audi Q7 -> 200 : 100000',
  33. 'Montana -> Audi Q7 -> 200000 : 1',
  34. 'Montana -> Qgodas -> 20000 : 0.2',
  35. 'Montana -> Chereshas -> 1000 : 0.3']
  36. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement