Hello,
Just wondering would anyone have any hints as to what I am doing wrong here? the program seems to act as if the “2468” object does not contain a “tracks” property when it does.evidence of this can be seen in the " if (!collection[id][propVar])" statement firing and if this statement(which creates an empty property) is removed Type error: x is undefined gets thrown up.
no idea what Im doing wrong here. help appreciated thankyou :
// 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) {
//set id
collection[id] = {};
var propVar = "tracks";
if (prop !== propVar && value !== ""){
collection[id][prop] = value;
}
if (!collection[id][propVar]){
collection[id][propVar] = [];
}
if (prop === propVar && value !==""){
collection[id].tracks.push(value);
}
if (value ===""){
delete collection[id][propVar];
}
return collection;
}
// Alter values below to test your code
updateRecords(2468, "tracks", "Free");