Free-code camp Inventory update solution

One other interesting problem with the inventory update challenge from freecodecamp. After this, it is going to be the modules for creating calculators and other challenges.


function updateInventoryWithArr(curInv, newInv) {
    var arr1 = curInv;
    var arr2 = newInv;
    var finalOutPutArr = [];
    var arr1Map = new Map();
    var arr2Map = new Map();
    var arr3Map = new Map();
    for (var j = 0; j < arr1.length; j++) {
        var data1 = arr1[j][1];
        var data2 = arr1[j][0];
        arr1Map.set(data1, data2);
        arr3Map.set(data1, data2);
    }

    for (var i = 0; i < arr2.length; i++) {
        var keyVal = arr2[i][0];
        var lookupKey = arr2[i][1];
        var keyData = arr1Map.get(lookupKey);
        var sumVal;
        if (typeof keyData != 'undefined') {

            sumVal = keyVal + keyData;
            arr3Map.set(arr2[i][1], sumVal);
            arr2Map.set(sumVal, arr2[i][1]);
        } else {
            arr3Map.set(arr2[i][1], keyVal);
            arr2Map.set(keyVal, arr2[i][1]);
        }

    }

    var dataInf = Array.from(arr3Map);
    for (var k = 0; k  b[1]
    });
    return finalOutPutArr;
}

Author: Sathish Jayapal

Indie programmer and blogger