t_sh0w

03. Store Provision

Feb 26th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printProductAndQuantity(stock, ordered) {
  2.   let collection = {};
  3.  
  4.   for (let i = 0; i < stock.length; i += 2) {
  5.     // i += 2, за да вземаме по 2 елемента наведнъж
  6.     collection[stock[i]] = Number(stock[i + 1]);
  7.   }
  8.  
  9.   for (let j = 0; j < ordered.length; j += 2) {
  10.     // проверка, ако стоката се повтаря
  11.     if (Object.keys(collection).includes(ordered[j])) {
  12.       // if (collection.hasOwnProperty(ordered[j])) {
  13.       collection[ordered[j]] += Number(ordered[j + 1]);
  14.     } else {
  15.       collection[ordered[j]] = Number(ordered[j + 1]);
  16.     }
  17.   }
  18.  
  19.   for (let key in collection) {
  20.     console.log(`${key} -> ${collection[key]}`);
  21.   }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment