0

I'm trying to add all the numbers for matching arrays and remove the duplicate names. It works for the first instance, but the while loop won't go past Apples.

function updateInventory(arr1, arr2) {

   function alphabetizer(a, b) {
   if (a[1] < b[1]) return -1;
   if (a[1] > b[1]) return 1;
   return 0;
   }

  var newInv = arr1.concat(arr2).sort(alphabetizer);


  for(var i = 0; i < newInv.length; i++) {
    while(newInv[i][1] === newInv[i++][1]) {
      newInv[i] += newInv[i++][0];
      newInv.push([newInv[i][0], newInv[i][1]]);
      newInv.splice(i,2);


   }
  } 

    return newInv;
}

// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"],
    [10, "Apples"]
];

var newInv = [
    [9, "Apples"],
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]
];

updateInventory(curInv, newInv);

It keeps giving me the error TypeError: newInv[(+ i)] is undefined But I am not sure why since i is defined, and if I just try to run newInv[i] I get the first result.

JS Fiddle link

Cody
  • 160
  • 12
  • 3
    You are incrementing `i` two times in the while loop by doing `i++`, you probably don't want to do that. You probably want `i + 1` instead – Get Off My Lawn Nov 22 '17 at 23:17
  • When doing that it tells me `i+1` is not defined. – Cody Nov 22 '17 at 23:23
  • 2
    it's not `i` that is undefined - it's `newInv[i + 1]` - because at the end of the loop i == netInv.length - 1, and i + 1 (newInv[newInv.length]) references an index that doesn't exist in the array ... hence, undefined – Jaromanda X Nov 22 '17 at 23:23
  • An extra info: https://stackoverflow.com/questions/971312/why-avoid-increment-and-decrement-operators-in-javascript – Victor Nov 22 '17 at 23:24

1 Answers1

1

There're couple places need fix. I updated this jsfiddle

function updateInventory(arr1, arr2) {

    function alphabetizer(a, b) {
        if (a[1] < b[1]) return -1;
        if (a[1] > b[1]) return 1;

        return 0;    
    }

    var newInv = arr1.concat(arr2).sort(alphabetizer);

    for (var i = 0; i < newInv.length; i++) {
        var j = i;
        while (newInv[j + 1] && newInv[j][1] == newInv[j + 1][1]) {
            newInv[j][0] += newInv[j + 1][0];
            newInv.splice(j + 1, 1);
            j++;
        }
    }

    document.getElementById('test').innerHTML = newInv.join('<br>');
}

// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"],
    [10, "Apples"]
];

var newInv = [
    [9, "Apples"],
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]
];

updateInventory(curInv, newInv);

https://jsfiddle.net/c3j8p2zu/7/

Hang Ye
  • 26
  • 2
  • Thank you! Could you explain why the j is needed? – Cody Nov 23 '17 at 03:22
  • You're increasing `i` and removing items at same time, which can skip some items in the array, `j` is for recording the old pointer position. And the main issue in the old code is `i++` could be bigger than the length of the array, hence `newInv[i++]` is undefined. – Hang Ye Nov 29 '17 at 23:28