Hi, I’ve been stuck in this code challenge for several days. I have read all the posts about it and I didn’t get anything. The explanation given in the challenge is not very enlightening. This is my code right now and with this I don’t get any green check.
function updateRecords(id, prop, value) {
if (prop === 'tracks' && collection[id]['tracks'] === undefined) {
collection.id['tracks'] = [];
} else if (prop != 'tracks' && value != '') {
collection.id[prop].push(value);
} else if (value === '') {
delete id.prop;
}
return collection;
}
You need to check for the value being passed. An empty value means you are expected to delete a property. Add a value checker to your first if-condition: if (value && prop === 'tracks' && collection[id]['tracks'] === undefined)
When you want to use a variable reference an object property, you MUST use the bracket notation. Change all the occurences of collection.id[prop] to collection[id][prop]. Also, since only the property tracks is an array, just set the properties with the = operator. Don’t use the Array method .push.
} else if (prop != 'tracks' && value != '') {
collection[id][prop] = value;
}
Fix your delete statement.
else if (value === '') {
delete collection[id][prop];
}
If value is not “”, then your must add the song to the album, even when it has no album:
Hi thanks for your answer! I changed my code based on what you told me and now I get some green checks but I still don’t get the logic. This is my code now:
function updateRecords(id, prop, value) {
if (value && prop === 'tracks' && collection[id]['tracks'] === undefined) {
collection[id]['tracks'] = [];
collection[id][prop].push(value);
} else if (prop != 'tracks' && value != '') {
collection[id][prop] = value;
} else if (value === '') {
delete collection[id][prop];
}
return collection;
}
In the first IF statement, how can value and prop be both ‘tracks’?
And in the second one why you do collection[id][prop] = value?
No. Because in this case, tracks is not an empty array. The last condition in your if-statement makes the whole block false. collection[id]['tracks'] === undefined
Therefore, it does not run the first statement for that case.