Hello all. I spent several hours trying to figure out this challenge before looking at the solution, but I’m afraid I still don’t understand it. I went through the explanation and tried to comment out the code so it makes sense to me, but I’m still puzzled by 2 sections (which I’ve marked with ? marks.)
In short, what is the purpose and what is the difference between:
collection[id][prop]=[value];
collection[id][prop] = value;
The entire code for reference:
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"
}
};
var collectionCopy = JSON.parse(JSON.stringify(collection));
function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") //Checks that Prop is "Tracks" and Value is not a blank string.
{
if(collection[id][prop]) {
collection[id][prop].push(value); //Adds Value to the Tracks array.
} else {
collection[id][prop]=[value]; //Value is updated? to the Tracks array.
}
} else if (value !== "") { //Checks if Value is not a blank string.
collection[id][prop] = value; //Value is updated? to the Tracks array.
} else {
delete collection[id][prop]; //Value is a blank string. Ergo removes the Prop from the object.
}
// console.log (collection[id][prop]);
return collection;
}
// Alter values below to test your code
console.log (collection);
updateRecords(5439, "artist", "ABBA");
console.log (collection);