Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(stock = [], ordered = []){
  2.  
  3.     function addingToArr(inStock = []){
  4.  
  5.         let inStockOutput = [];
  6.  
  7.         for(let i = 0; i < inStock.length; i++){
  8.  
  9.             if(i % 2 == 0){
  10.                 let name = inStock[i];
  11.                 let count = Number(inStock[i + 1]);
  12.  
  13.                 let product = { name, count };
  14.                 inStockOutput.push(product);  
  15.             }            
  16.         }
  17.         return inStockOutput;
  18.     }
  19.  
  20.     let firstArray = addingToArr(stock);
  21.     let secondArray = addingToArr(ordered);
  22.  
  23.     let finalResult = firstArray.slice();
  24.  
  25.     for (const first of firstArray) {
  26.         for (const second of secondArray) {
  27.             if(first.name === second.name){
  28.                 first.count += second.count;
  29.             }
  30.             else{
  31.                 finalResult.push(second)
  32.             }
  33.         }
  34.     }
  35.  
  36.     console.log(finalResult)
  37. }
  38.  
  39. solve([
  40.     'Chips', '5', 'CocaCola', '9', 'Bananas', '14', 'Pasta', '4', 'Beer', '2'],
  41.     [
  42.     'Flour', '44', 'Oil', '12', 'Pasta', '7', 'Tomatoes', '70', 'Bananas', '30'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement