Advertisement
kstoyanov

03. Cappy Juice

Sep 21st, 2020 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function cappyJuice(input) {
  2.   const quantities = {};
  3.   const bottles = {};
  4.  
  5.   input.forEach((line) => {
  6.     const tokens = line.split(' => ');
  7.     const [fruit, quantity] = tokens;
  8.  
  9.     if (!quantities.hasOwnProperty(fruit)) {
  10.       quantities[fruit] = 0;
  11.     }
  12.  
  13.     quantities[fruit] += Number(quantity);
  14.     if (quantities[fruit] >= 1000) {
  15.       bottles[fruit] = parseInt(quantities[fruit] / 1000);
  16.     }
  17.   });
  18.  
  19.   Object.keys(bottles).forEach((key) => {
  20.     console.log(`${key} => ${bottles[key]}`);
  21.   });
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement