I managed to complete the lesson by understanding the logic behind the hints and a quick peek to the results given, which brings some confusion… I understand the concept of the exercise, however, I can’t understand something:
When creating actions like “delete records[id][prop]” or “records[id][prop].push(value)”, how does the function know that [id] and [prop] are referring to the Element (ID) inside the object and then the Property (Prop) is whatever property we assign to that parameter.
I know that we are assigning the parameters inside the function’s parenthesis but I don’t see the relation of when “id” became the element/property. Again, I know we assign the parameter but I’m not sure how the function knows that we are referring to that.
I’m not sure if I’m explaining myself properly but it’s as good as I can let it out.
A million thanks in advance.
**Your code so far**
// Setup
const recordCollection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
if (prop !== 'tracks' && value !== "") {
records[id][prop] = value;
}
else if (prop === 'tracks' && records[id].hasOwnProperty("tracks") === false) {
records[id][prop] = [value];
}
else if (prop === 'tracks' && value !== "") {
records[id][prop].push(value);
}
else if (value === "") {
delete records[id][prop];
}
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.60
Challenge: Record Collection
Link to the challenge: