Basic JavaScript: Record Collection test condition

I have and understand most of the solution, but am confused as to why one of the tests passes with this code.

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));

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

return collection;
}

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


Since the "artist" property does not exist under id "5439", why would this pass the test?  Shouldn't the "artist" property require being set/initialized, which is different than updating a property that exists?

If I follow the if statement logic, this test would first fall under the following else if statement:

else if (value !== “”) {
collection[id][prop] = value;

I would assume the code should be the following to initialize that property of the collection array. But the test passes without the brackets. Shouldn’t that only work with properties that already exist?

   else if (value !== "") {
    collection[id][prop] = [value];

[value] is an array, and the only property that has an array as value is "tracks", so it’s right if any other property is set as value, and not [value]

1 Like

Thank you for the explanation. It’s very easy to overlook the simple things and dig too deep.