Record Collection please advise!

I almost have it completed. This has been a hard one for me. many hours spent.The only one that doesn’t pass is:

“After updateRecords(2468, "tracks", "Free") , tracks should have "1999" as the first element.”

I believe this is because the creation of the new array erases the existing array along with the data it contains. I’m not sure how to implement the code to copy the existing items in the array and add it to the new array.

Your code so far


// 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"
}
};

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

return collection
}

// Alter values below to test your code
updateRecords(1245, "album", "Riptide");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

Challenge: Record Collection

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

here you overwrite the current value of the array to an empty array

there is a way in which you can check if the property already exist (if it already exists you need to leave it as it is), well, more than one, at least a couple from material you recently learned. And you need to set it to an empty array only if there is no array yet.

I found the solution! Thanks for your advice. Part of my issue is that I skipped this lesson and then decided to come back to it so the hasOwnProperty() method was not fresh in my mind. Still I had a bit of trouble trying to make it work. Just needed to add in some ‘else if’ statements. Once again, thanks.

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