Advertisement
ErolKZ

Untitled

Sep 15th, 2021
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1.  
  2. function updateInventory(arr1, arr2) {
  3.  
  4.  
  5.  
  6.  
  7. arr1.map(par => {
  8.  
  9.  
  10. for (let i = 0; i < arr2.length; i++) {
  11.  
  12. if (par[1] === arr2[i][1]) {
  13.  
  14. par[0] = par[0] + arr2[i][0];
  15.  
  16. }
  17.  
  18. }
  19.  
  20. });
  21.  
  22.  
  23.  
  24.  
  25. arr1 = arr1.concat(arr2);
  26.  
  27.  
  28. arr1 = arr1.sort((firstEl, secondEl) => {
  29.  
  30. return firstEl[1][0].charCodeAt(0) - secondEl[1][0].charCodeAt(0);
  31.  
  32. });
  33.  
  34.  
  35.  
  36.  
  37. for (let i = 0; i < arr1.length - 1; i++) {
  38.  
  39.  
  40. if (arr1[i][1] === arr1[i + 1][1]) {
  41.  
  42. if (arr1[i][0] > arr1[i + 1][0]) {
  43.  
  44. arr1.splice(i + 1, 1);
  45.  
  46. } else if (arr1[i][0] < arr1[i + 1][0]) {
  47.  
  48. arr1.splice(i, 1);
  49.  
  50. } else {
  51.  
  52. arr1.splice(i, 1);
  53.  
  54. }
  55.  
  56.  
  57.  
  58. }
  59.  
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. return arr1;
  70.  
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77. // Example inventory lists
  78.  
  79. var curInv = [[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]];
  80.  
  81. var newInv = [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]];
  82.  
  83.  
  84.  
  85. console.log(updateInventory(curInv, newInv));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement