Tell us what’s happening:
I am convinced that my code should work. Running all the tests locally returns the expected result, but the test won’t accept it as the right answer. It claims my function does not return an array, yet two of the tests pass (the ones which involve one input). Please help!
Your code so far
function updateInventory(current, incoming) {
// function to sort array alphabetically by item
function sortArray(arr) {
return arr.sort((a, b) => a[1].localeCompare(b[1]));
}
// if either input is empty, just return the other, sorted
if (incoming.length === 0) {
return sortArray(current);
}
if (current.length === 0) {
return sortArray(incoming);
}
// function to turn arrays into objects with key-value pairs
var objectify = function (inv) {
return inv.reduce((acc, el) => {
acc[el[1]] = el[0];
return acc;
}, {});
};
// new stock and delivery transformed into objects
stock = objectify(current);
delivery = objectify(incoming);
// merge stock and delivery values
for (let item in delivery) {
stock[item] = delivery[item] + stock[item] || delivery[item];
}
// translate stock object back to array with correct format
var answer = [];
for (let item in stock) {
answer.push([stock[item], item]);
}
// sort answer alphabetically by item name
return sortArray(answer);
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36
.
Challenge: Inventory Update
Link to the challenge: