Tell us what’s happening:
Hey guys! First of all I’m really happy I discovered this platform where I could further expand my knowledge(mostly of JS I’m familiar with HTML/CSS but not very interested in them).
Anyways I just finished the Inventory Update and was wondering if my my solution was too simple for the problem. I’m fairly good in Lua and I just took the easiest and fastest approach I could think off.
Your code so far
function updateInventory(arr1, arr2) {
//Join both arrays
for (var key in arr2) {
arr1.push(arr2[key]);
}
//Add up recurring items and remove duplicates
for (var key in arr1) {
for (var otherKey in arr1){
if (arr1[key][1] === arr1[otherKey][1] && key != otherKey){
arr1[key][0] += arr1[otherKey][0];
arr1.splice(otherKey, 1);
}
}
}
//Sort alphabetically
arr1.sort(compareSecondColumn);
function compareSecondColumn(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}
return arr1;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/inventory-update/