Advertisement
_Lyuba_Ivanova_

CappyJuices

Oct 3rd, 2020 (edited)
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let bottles = new Map();
  4.     let juiceStorage = {};
  5.  
  6.     input.forEach(juice => {
  7.         let [type, quantity] = juice.split(' => ');
  8.         quantity = Number(quantity);
  9.  
  10.         if (!juiceStorage[type]) {
  11.  
  12.             juiceStorage[type] = quantity;
  13.  
  14.         } else {
  15.  
  16.             juiceStorage[type] += quantity;
  17.            
  18.         }
  19.         if (juiceStorage[type] >= 1000) {
  20.  
  21.             if (!bottles.has(type)) {
  22.  
  23.                 bottles.set(type, []);
  24.                 bottles.get(type).push(quantity);
  25.  
  26.             } else {
  27.  
  28.                 bottles.get(type).push(quantity);
  29.  
  30.             }
  31.         }
  32.     });
  33.     Array.from(bottles).forEach(el => {
  34.         let quantity = parseInt((el[1].reduce((a, b) => a + b, 0)) / 1000);
  35.         console.log(`${el[0]} => ${quantity}`);
  36.     });
  37.  
  38. }
  39. solve(['Kiwi => 234',
  40.     'Pear => 2345',
  41.     'Watermelon => 3456',
  42.     'Kiwi => 4567',
  43.     'Pear => 5678',
  44.     'Watermelon => 6789'
  45. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement