Inventory Update - works with node.js not in FCC

Tell us what’s happening:
I’ve tried this 2 different ways one with es5 and one with es6. Both work with node.js I tried to use node.js 8 with the es5 version which i’ve posted here. It works in that environment but not in FCC test.

Any ideas?

Your code so far

'use strict';
function updateInventory (curInv, newInv) {
  [curInv, newInv] = combineInventory(curInv, newInv);
  curInv = filterNull(curInv);
  let newArray = mergeArray(curInv, newInv);
  newArray = sortArray(newArray);
  console.log(newArray);
  return newArray;
}

// combine inventory for items that appear 2x and set old inv to null
function combineInventory (curInv, newInv) {
  curInv.forEach((innerCurrentArray, index) => {
    newInv.forEach((innerNewArray, index2) => {
      if (innerCurrentArray[1] === innerNewArray[1]) {
        curInv[index][1] = null;
        newInv[index2][0] = innerCurrentArray[0] + innerNewArray[0];
      }
    });
  });
  return [curInv, newInv];
}
// remove items set to null
function filterNull (curInv) {
  return curInv.filter(item => !!item[1]);
}

// combine arrays
function mergeArray (curInv, newInv) {
  return curInv.concat(newInv);
}
// sort arrays
function sortArray (array) {
  return array.sort((a, b) => {
    return a[1].toUpperCase() > b[1].toUpperCase();
  });
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36.

Link to the challenge:

You have a syntax error. There is an extra } on line 32.

Thanks for the quick reply but I do not see any syntax errors. You sure you copied it right?

You didn’t delete the curInv or newInv, did you?

@ArielLeslie curInv and newInv are params. You sure they need to be deleted? Don’t they just get reset with each call of updateInventory?

They should not be deleted. The only part that you should be replacing with you copy-paste is the updateInventory function.

I think you’re saying I should have deleted vars newInv and curInv below the original updateInventory declaration. I did that. The way I’ve pasted the code here in this post is the way I have it in FCC and locally.

No. I have said the exact opposite of that twice.

I sense some hostility sorry If I have upset you somehow but if you can elaborate it would be much appreciated. I never had example inventory curInv and newInv in my final code. I don’t think that’s the issue.

Reset the code in the challenge. You will see the values curInv and newInv. Do no delete them.

@ArielLeslie It did not work.

What is your complete solution that you are trying to pass the challenge with?

'use strict';
function updateInventory (curInv, newInv) {
  [curInv, newInv] = combineInventory(curInv, newInv);
  curInv = filterNull(curInv);
  let newArray = mergeArray(curInv, newInv);
  newArray = sortArray(newArray);
  console.log(newArray);
  return newArray;
}

// combine inventory for items that appear 2x and set old inv to null
function combineInventory (curInv, newInv) {
  curInv.forEach((innerCurrentArray, index) => {
    newInv.forEach((innerNewArray, index2) => {
      if (innerCurrentArray[1] === innerNewArray[1]) {
        curInv[index][1] = null;
        newInv[index2][0] = innerCurrentArray[0] + innerNewArray[0];
      }
    });
  });
  return [curInv, newInv];
}
// remove items set to null
function filterNull (curInv) {
  return curInv.filter(item => !!item[1]);
}

// combine arrays
function mergeArray (curInv, newInv) {
  return curInv.concat(newInv);
}
// sort arrays
function sortArray (array) {
  return array.sort((a, b) => {
    return a[1].toUpperCase() > b[1].toUpperCase();
  });
}

// 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);

That code passes all the tests.

Not for me for some reason.

That’s so weird I tried on safari and Firefox and it works which is great. It seems it’s a bug with chrome. @ArielLeslie thanks for helping me. I thought I was going crazy. I wrote the code for this 3 different ways and they worked with node.js each time but not in FCC.

If you’re on Chrome it might be failing because your sort function does not return the correct value. I happened to test it in Firefox, which uses a non-standard implementation of sort().

Got it. Good to know. I will research sort function in chrome so i don’t have bug like this in prod code. One thing that’s weird is if Node.js is a fork of chrome’s v8 engine why does this code not work in chrome.

Look at the return values of the compare function. You are currently returning a boolean.

1 Like

Thanks I changed my sort function and it fixed the issue in Chrome. Thanks so much for your help!

function sortArray (array) {
  return array.sort((a, b) => {
    return a[1].toUpperCase() > b[1].toUpperCase() ? 1 : -1;
  });
}