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.
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.
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.
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.