Problemos with Object properties in Record Collection

Hi,

So I was trying to take this challenge as an opportunity to improve my code and use early returns. It isn’t as good as that of others I’ve seen on the forum, but it’s an improvement on infinite if statements.

However I have a problem. It works for every instance except the artist for Id 5439, and the album for Id 1245. Additionally, I tried console logging on line 42, and it doesn’t just return ‘Dancing Queen’, but ‘ABBA’, and ‘Riptide’ as well. I know this suggests there’s some kind of fundamental error in the entire section of the code dealing with prop-creation, but I can’t find it.

If you have suggestions on how to improve my code beyond just this error, then please leave them as well. Thank you for your time.

My 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 (!value && !collection[id][prop]){
  return collection;
  }
if (!value && collection[id][prop]){
  delete collection[id][prop];
  return collection;
  }

if (!collection[id][prop]){
    if (prop="tracks"){
      collection[id][prop] = [];
      collection[id][prop].push(value);
      console.log(collection[id]["tracks"]);
    } 
    
    if (prop!="tracks"){
      collection[id][prop]=value;
    }
  return collection;
  }

if (collection[id][prop]){
  if (prop="tracks"){
    collection[id][prop].push(value);
  } else{
    collection[id][prop] = value;
  }
  return collection;
}
}

// Alter values below to test your code
updateRecords(5439, "tracks", "Dancing Queen");

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

That fixed it, thank you. Comparison was my intention, but it is interesting that you can assign within an if statement. I wonder if there’s any use for that.