Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cappy(input = []) {
- const bottles = new Map();
- input.reduce((acc, curr) => {
- let [juice, quantity] = curr.split(" => ");
- quantity = Number(quantity);
- if(!acc.hasOwnProperty(juice)) {
- acc[juice] = 0;
- }
- acc[juice] += quantity;
- if (acc[juice] > 1000) {
- if (!bottles.has(juice)) {
- bottles.set(juice, 0);
- }
- bottles.set(juice, bottles.get(juice) + parseInt(acc[juice] / 1000));
- acc[juice] %= 1000;
- }
- return acc;
- }, {});
- for(let[juice, quantity] of bottles) {
- console.log(`${juice} => ${quantity}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement