Out put is correct but FCC editor rejecting it , kindly help

Hi,

For the task - https://www.freecodecamp.org/challenges/inventory-update, i came up with a solution which is giving right output but FCC editor is rejecting it can you please guide what i need to do here … thanks

//logic - just compare and see if value like Bowling Ball,matches, it so add their numbers and then push them in new array , then remove the values from both , left with unique values in each array now, , merge them and then ,merge with array which had similar values array with added values of items …

My JSFiddle Link - https://jsfiddle.net/jinisner/0j7rfbyq/1/


function updateInventory(arr1, arr2) {
    var test = [];
    var res = [];
    var fin=[];
    var i,j;
    for( i = 0; i < curInv.length; i++){
        for( j = 0; j < newInv.length;j++){
            if(curInv[i][1] === newInv[j][1]){
                curInv[i][0] = curInv[i][0] + newInv[j][0];
                test.push(curInv[i]);
                curInv.splice(i,1);
                newInv.splice(j,1);
                res = curInv.concat(newInv);
              
            }
        
             
       }
        
         
   }
      
    fin = test.concat(res);
    console.log(fin);  
    return fin;
}

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

Your code only works once. Look at what happens when I try to run all the tests in sequence: https://repl.it/LPRX/0

@ArielLeslie - Thanks, i am totally confused and exhausted will take a break and try again later …

Always a good idea. Go recharge and come back with fresh eyes.

1 Like

@ArielLeslie - Thanks, i solved it finally :), i had several issues, one i was not catering to the condition where if one of the array was empty, then i started pushing the whole 2d array in two an array which resulted in 3d array , so i then pushed each item at a time …

2)I was not able to sort , then i came across , comparison option inside of sort , it does not explain a whole lot just that if a < b or a should be ahead of b then it will return -1 or else 1 , very confusing but it does the job, … then i kept rest logic simple as before, just put all repeat values in different array , then remove the similar one’s …

Finally concat the two initial array with non similar values left now, and then concat with the one with similar value added, finally do a sort … My JSFiddle link - https://jsfiddle.net/jinisner/w8htphy2/


function updateInventory(arr1, arr2) {
  
    var test = [];
    var res = [];
    var fin=[];
     var i,j;
    
    if(arr1.length === 0){
       for(var i = 0; i < arr2.length; i++){
           arr1.push(arr2[i]);
           arr1.sort(function(a,b){
               if(a[1] < b[1]){
                   return -1;
               }
               else{
                   return 1;
               }
            });
       }
       
        return arr1;
    }
    else if(arr2.length === 0){
        for(var j = 0; j < arr1.length; j++){
            arr2.push(arr1[j]);
           arr2.sort(function(a,b){
              if(a[1] < b[1] ){
                  return -1;
              }
               else{
                   return 1;
               }
           });
         }
       
        return arr1;
    }
    
    
     for( i = 0; i < arr1.length; i++){
        for( j = 0; j < arr2.length;j++){
            if(arr1[i][1] === arr2[j][1]){
                arr1[i][0] = arr1[i][0]  + arr2[j][0];
                test.push(arr1[i]);
                arr1.splice(i,1);
                arr2.splice(j,1);
               }
             res = arr1.concat(arr2);
           
         }
       }
   fin = res.concat(test).sort(function(a,b){
       if(a[1] < b[1]){
           return -1;
       }
       else{
           return 1;
       }
   });
   return fin;  
    
}

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

Good job! I know this can get frustrating, so congratulations on not letting that get to you!