// 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];
return collection;
}
if (prop !== "tracks" && value !== "") {
collection[id][prop] = value;
}
else {
if (!collection[id].hasOwnProperty("tracks")) {
collection[id].tracks = [];
collection[id].tracks.push(value); //is there a better way to code this?
}
else {
collection[id].tracks.push(value); //feel like I shouldn't be repetitive here...
}
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "tracks", "Take a Chance on Me");
After a lot of blood, sweat and tears I passed this waypoint with the above code. However I have a feeling It’s not quite right. Lines 45 & 48 are identical as it was the only way to pass all the validations. Is there a better, “don’t repeat yourself” way to do this?
Thanks for any insight!