2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: a56138aff60341a09ed6c480
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Inventory Update
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Description
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'description' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in < code > arr1< / code > ). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Remember to use < a  href = 'http://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514'  target = '_blank' > Read-Search-Ask< / a >  if you get stuck. Try to pair program. Write your own code.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Instructions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'instructions' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Tests
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'tests' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```yml
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								tests:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: The function < code > updateInventory</ code >  should return an array.
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    testString: assert.isArray(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), 'The function < code > updateInventory< / code >  should return an array.');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</ code >  should return an array with a length of 6.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    testString: assert.equal(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]).length, 6, '< code > updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])< / code >  should return an array with a length of 6.');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</ code >  should return < code > [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]</ code > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]], '< code > updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])< / code >  should return < code > [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]< / code > .');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])</ code >  should return < code > [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]</ code > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], []), [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], '< code > updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])< / code >  should return < code > [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]< / code > .');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</ code >  should return < code > [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]</ code > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    testString: assert.deepEqual(updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]], '< code > updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])< / code >  should return < code > [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]< / code > .');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  text: < code > updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])</ code >  should return < code > [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]</ code > .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    testString: assert.deepEqual(updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]]), [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]], '< code > updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])< / code >  should return < code > [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]< / code > .');
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:37:37 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Challenge Seed
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'challengeSeed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< div  id = 'js-seed' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function updateInventory(arr1, arr2) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // All inventory must be accounted for or you're fired!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return arr1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// 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);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / div >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Solution
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< section  id = 'solution' >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function updateInventory(arr1, arr2) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  arr2.forEach(function(item) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    createOrUpdate(arr1, item);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // All inventory must be accounted for or you're fired!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return arr1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function createOrUpdate(arr1, item) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  var index = -1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  while (++index <  arr1.length )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (arr1[index][1] === item[1]) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      arr1[index][0] += item[0];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (arr1[index][1] > item[1]) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  arr1.splice(index, 0, item);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// 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);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / section >