Inventory Update not passing despite correct answer

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:

Your code is not returning the correct value.
You are creating a nested array of length 1.

Thank you for your reply. I don’t understand what you mean, though. Adding .length to my answer array I get 6, as expected.

Sorry. My bad. I had a pre-coffee reading error.

You are using undeclared variables. freeCodeCamp always uses strict mode when running your code.

Add

"use strict"

To the top of your JavaScript and you’ll see the problem in the environment you are running in.

1 Like

Aaaahahaha thank you! That was a relic of when I was mutating the inputs which were also called stock and delivery. Lesson learned, thanks for your help :smiley:

Glad I could help. Happy coding!