Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function printStock(stock, orderedProducts) {
- let currentStock = {};
- stock.forEach(e => {
- let index = stock.indexOf(e);
- if (index % 2 == 0) {
- if (!currentStock[e]) {
- currentStock[e] = Number(stock[index + 1]);
- } else {
- currentStock[e] += Number(stock[index + 1]);
- }
- }
- });
- orderedProducts.forEach(el => {
- let index = orderedProducts.indexOf(el);
- if (index % 2 == 0) {
- if (!currentStock[el]) {
- currentStock[el] = Number(orderedProducts[index + 1]);
- } else {
- currentStock[el] += Number(orderedProducts[index + 1]);
- }
- }
- });
- // for (let i = 0; i < orderedProducts.length; i += 2) {
- // let element = orderedProducts[i];
- // if (currentStock[element]) {
- // currentStock[element] += +orderedProducts[i + 1];
- // } else {
- // currentStock[element] = +orderedProducts[i + 1];
- // }
- // }
- Object.keys(currentStock)
- .forEach(product => console.log(`${product} -> ${currentStock[product]}`));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement