Advertisement
miroLLL

CAPPY

Jun 5th, 2020
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveCurrentProblem(input) {
  2.  
  3.     let map = new Map();
  4.     let remaining = new Map();
  5.  
  6.     for (let productInfo of input) {
  7.  
  8.         let [product, quantity] = productInfo.split(/\s+=>\s+/);
  9.  
  10.         if (quantity >= 1000 || (quantity + remaining.get(product) >= 1000) || (quantity + map.get(product) >= 1000)) {
  11.  
  12.             let left = remaining.get(product) !== undefined ? Number(remaining.get(product)) : 0;
  13.  
  14.             if(map.has(product) === false){
  15.                 map.set(product, 0);
  16.  
  17.             }
  18.             map.set(product, map.get(product) + +quantity + left);
  19.             remaining.delete(product);
  20.  
  21.         } else {
  22.             if(remaining.has(product) === false){
  23.                 remaining.set(product, 0);
  24.             }
  25.  
  26.             remaining.set(product, Number(remaining.get(product)) + +quantity);
  27.         }
  28.     }
  29.  
  30.     Array.from(map.keys()).forEach(fruit => {
  31.         let bottles = Math.floor(map.get(fruit) / 1000);
  32.         console.log(`${fruit} => ${bottles}`);
  33.     })
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement