Not able to pass the test of adding tranks as the last element!

I was just going through my course when I got stuck at these challenge .I tried using all things I know but still its not working out.!
Pls help!!

Here is my code:-


// Setup
var collection = {
 2548: {
   albumTitle: 'Slippery When Wet',
   artist: 'Bon Jovi',
   tracks: ['Let It Rock', 'You Give Love a Bad Name']
 },
 2468: {
   albumTitle: '1999',
   artist: 'Prince',
   tracks: ['1999', 'Little Red Corvette']
 },
 1245: {
   artist: 'Robert Palmer',
   tracks: []
 },
 5439: {
   albumTitle: 'ABBA Gold'
 }
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
if(prop!='tracks' && value!="")
{
 object[id][prop]=value;
 
}
else if(value==="")
 {
    delete object[id][prop];
 }
else
 {
  if(collection[id].hasOwnProperty("tracks"))
   {  
    collection[id].tracks.push(value);    

   }
 
  else
  {
   collection[id].tracks=[];
   collection[id].tracks.push(value); 
  }


}
return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Challenge: Record Collection

**Link to the challenge:**https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

for a part of your function you correctly use the function parameter object, and then suddently you start using the global object collection, meaning that the object passed in as argument by the tests is not changed by those lines

1 Like

Should i use object or collectiond and what’s the difference between two?Can u please elaborate a little. :sweat_smile:
I replace collection by object and it worked!

object is the function parameter, meaning it “refers” to whatever value is passed to the function; while collection is the global object defined outside your function.

The main difference, and reason why the behavior changes is that you are not 100% guaranteed that object will always refer to collection.

For example, what if under the hood, freecodecamp pass a different argument to your function during the testing fase? All of a sudden the behavior of your function changes.

Hope this helps :sparkles:

1 Like

object is the function parameter

you can see it in the function definition

this means that the function could be called with any object, and to keep the function reusable, you can’t reference global variables like collection

2 Likes