CODING INTERVIEW PREP - Inventory Update - SOLUTION SUGGESTION

Dear Sir or Madame,

I had a different solution to the problem which I think might interest you.
Generally I try to solve every looping with the array functions .filter(), .map() and .reduce(). I am from Hungary so at some point I might made some mistakes in the explanation, feel free to correct it if you found my solution worth to post it as a sample solution.

Yours faithfully
Nándor MIHÁLY

Solution
function updateInventory(arr1, arr2) {
    // first of all map through the curInv array to either preserve or update the count of its elements
    let arr = arr1.map(currInvElement => {
        return !arr2.some(element => element[1] === currInvElement[1]) // if there is no element in the newInv array, that matches the curInv array's element
                 ? currInvElement // then preserve the element as it is
                 : [ 
                    currInvElement[0] + arr2.filter(newInvElement => currInvElement[1] == newInvElement[1])[0][0], //otherwise find the matching elements in both arrays and sum their count as the 0th element of the array
                    currInvElement[1] // and take the name of the element as the 1st element of the returned array
                    ]
    })

    // now let's add the items which are only in the newInv array
    arr = arr.concat(arr2
        .filter(elem => !arr.some(el => el[1] == elem[1])))
        .sort((a, b) => { //then sort them alphabetically
        return a[1] === b[1]
                ? 0
                : a[1] < b[1]
                    ? -1
                    : 1 });
    console.log(arr); // check your result
    return arr;
}

// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"], 
    [1, "Hair Pin"], 
    [5, "Microphone"]
];

var newInv = [
    [2, "Hair Pin"], 
    [3, "Half-Eaten Apple"], 
    [67, "Bowling Ball"], 
    [7, "Toothpaste"]
];

updateInventory(curInv, newInv);

Hi @mihaly.nandor95 !

I edited your post to include the challenge link and I formatted your code.
I also moved your post over to the #contributors section.

Please read the following instructions on how to make guide post suggestions in the future.


Thank you, for your contribution. For future contributions, please wrap your solution within :

[details]
```
code goes here...
```
[/details]

Also, provide all of the necessary code to pass the challenge.

Also, when you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor ( </> ) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

const updateInventory = (arr1, arr2) => {
    return arr1.concat(arr2).reduce((invent, curr) => {
        if (invent.some(item => item[1] === curr[1])) {
            const index = invent.findIndex(i => i[1] === curr[1])

            invent[index][0] += curr[0]
        } else {
            invent.push(curr)
        }


        return invent.sort((a, b) => (a[1] > b[1] ? 1 : -1))
    }, [])
}

Please don’t just dump solutions without any discussion into old threads. Thanks.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.