Basic JavaScript: Record Collection - Issue with one test

Hey guys,
I don’t know why my code doesn’t pass all the tests for the Record Collection challenge. Could you please help me :slight_smile:
And also explain what I did wrong.

My code doesn’t pass the following test:
After updateRecords(2468, “tracks”, “Free”), tracks should have “1999” as the first element.

Here is a link to the challenge:
</javascript-algorithms-and-data-structures/basic-javascript/record-collection/>
(Note: I cannot post a link here yet)

Here is the code:

// Setup
var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    },
    "5439": {
      "album": "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

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

  return collection;
}

// Alter values below to test your code
updateRecords(2468, "tracks", "Free");

At first, here is a mistake, you should call collection[id].hasOwnProperty("tracks"). Your code is pretty messy and I tried to arrange it. You have so much “if” and you should keep the rules order.
Here you have the solution but you can try again to do it based on the tips above.

Summary
function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== ""){
      if(collection[id].hasOwnProperty("tracks")) collection[id][prop].push(value);
      else collection[id][prop] = [value];
  }
  else if (value !== "") {
    collection[id][prop] = value; 
  }
  else{
    delete collection[id][prop];
  }
  return collection;
}

You haven’t accounted for the case of updateRecords(-1, ...), as in what if the given record doesn’t exist? Put this check before any of your other conditions.

Also, your prop_push function (which really isn’t necessary) should be pushing value, not [value].