Liliana797979

Storage - fundamentals

Sep 5th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.   let map = new Map();
  3.   for (let string of input) {
  4.     let tokens = string.split(' ');
  5.     let product = tokens[0];
  6.     let quantity = Number(tokens[1]);
  7.     if (!map.has(product)) {
  8.       map.set(product, quantity);
  9.     } else {
  10.       let currQuantity = map.get(product);
  11.       let newQuantity = currQuantity += quantity;
  12.       map.set(product, newQuantity);
  13.     }
  14.   }
  15.   for (let key in map) {
  16.     console.log(`${key} -> ${map[key]}`);
  17.   }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment