Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2020
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. function cappy(input = []) {
  2. const bottles = new Map();
  3.  
  4. input.reduce((acc, curr) => {
  5. let [juice, quantity] = curr.split(" => ");
  6. quantity = Number(quantity);
  7.  
  8. if(!acc.hasOwnProperty(juice)) {
  9. acc[juice] = 0;
  10. }
  11. acc[juice] += quantity;
  12.  
  13. if (acc[juice] > 1000) {
  14. if (!bottles.has(juice)) {
  15. bottles.set(juice, 0);
  16. }
  17.  
  18. bottles.set(juice, bottles.get(juice) + parseInt(acc[juice] / 1000));
  19. acc[juice] %= 1000;
  20. }
  21.  
  22. return acc;
  23. }, {});
  24.  
  25.  
  26. for(let[juice, quantity] of bottles) {
  27. console.log(`${juice} => ${quantity}`);
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement