Advertisement
Guest User

Untitled

a guest
Oct 19th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printStock(stock, orderedProducts) {
  2.     let currentStock = {};
  3.  
  4.     stock.forEach(e => {
  5.         let index = stock.indexOf(e);
  6.         if (index % 2 == 0) {
  7.             if (!currentStock[e]) {
  8.                 currentStock[e] = Number(stock[index + 1]);
  9.             } else {
  10.                 currentStock[e] += Number(stock[index + 1]);
  11.             }
  12.         }
  13.     });
  14.  
  15.     orderedProducts.forEach(el => {
  16.         let index = orderedProducts.indexOf(el);
  17.         if (index % 2 == 0) {
  18.             if (!currentStock[el]) {
  19.                 currentStock[el] = Number(orderedProducts[index + 1]);
  20.             } else {
  21.                 currentStock[el] += Number(orderedProducts[index + 1]);
  22.             }
  23.         }
  24.     });
  25.  
  26.     // for (let i = 0; i < orderedProducts.length; i += 2) {
  27.     //     let element = orderedProducts[i];
  28.     //     if (currentStock[element]) {
  29.     //         currentStock[element] += +orderedProducts[i + 1];
  30.     //     } else {
  31.     //         currentStock[element] = +orderedProducts[i + 1];
  32.     //     }
  33.     // }
  34.  
  35.     Object.keys(currentStock)
  36.         .forEach(product => console.log(`${product} -> ${currentStock[product]}`));
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement