1

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(i=0;i<newInv.length;i++){ while(newInv[i][1] != -1){ newInv[i][0] += newInv[i+1][0]; newInv.push([newInv[i][0], newInv[i][1]]); newInv.splice(i,2); return newInv; } return newInv; } 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); 

So when I run this I just end up getting

[[21,"Bowling Ball"],[67,"Bowling Ball"],[2,"Dirty Sock"],[1,"Hair Pin"],[2,"Hair Pin"],[3,"Half-Eaten Apple"],[5,"Microphone"],[7,"Toothpaste"],[19,"Apples"]] 

2 Answers 2

3

The problem is because you are returning from the while loop. It terminates the further execution of the loop. Remove return part from the for and while loops. And also declare variable i with var keyword to make it scoped to the function, not global.

for(var i = 0; i < newInv.length; i++) { while(newInv[i][1] != -1) { newInv[i][0] += newInv[i+1][0]; newInv.push([newInv[i][0], newInv[i][1]]); newInv.splice(i,2); } } 
Sign up to request clarification or add additional context in comments.

7 Comments

I did it like this, but was getting 'TypeError: newInv[i] is undefined' even after adding the 'var' which is what I think I was missing earlier. Adding in the return gets rid of this error.
It can not be related to the return. Your return just terminates the function
I didn't think so. I was just pointing out that it does not throw that error WHEN i have the return in there.
Maybe when an item except the first has undefined, so when you have return it does not reach to that element, but when you remove it iterates and reaches
I'm a bit confused then, since its saying the newInv[i + 1] is not defined. But if I change it to newInv[i++] it says newInv[i] meaning the one before it, is undefined. So I'm not sure why it would be saying that either of those are undefined, since i is declared right before it at a var.
|
0

This got it working, adding var j = i and changing some code.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.