Advertisement
TZinovieva

Storage JS Fundamentals

Mar 1st, 2023
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function storage(arr) {
  2.     let store = new Map();
  3.  
  4.     for (let line of arr) {
  5.         let tokens = line.split(' ');
  6.         let product = tokens[0];
  7.         let quantity = Number(tokens[1]);
  8.  
  9.         if (store.has(product)) {
  10.             let currentQuantity = store.get(product); // запазваме текущото количество
  11.             let newQuantity = currentQuantity += quantity;
  12.             store.set(product, newQuantity);
  13.         } else {
  14.             store.set(product, +quantity);
  15.         }
  16.     }
  17.     for (let line of store) {
  18.         console.log(`${line[0]} -> ${line[1]}`);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement