Record Collection - Can't solve this :(

Hey There!

First post ever, so please go easy on me :slight_smile:

Just started my adventure coding with javascript, and am going through the basic tutorial here on freecodecamp and have run into a wall in this project.

Can anyone shed light why this isn’t working?

Thank you so much!


// 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 (value == "") {
  delete collection[id][prop];
}
else if (prop != "tracks" && value != "") {
  collection[id][prop] = value;

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

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

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

the first condition that is true is the one executed (for the same reason as you have as first condition value == "" every value != "" after that is unnecessary)
so if the prop is "tracks" the first condition I quoted is true and it is the one executed - you never check if the property exist in the object, so you just push to the array even if the array doesn’t exist

Thank you so much! Makes perfect sense.

Getting used to tracking my entire logic structure as well.