Confused on how this exercise works

  • Your function must always return the entire record collection object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .

Says to update prop to value, how is “record[id][prop] = value;” the answer? Where does record and id come from? It doesn’t say in the question.

// 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’);

From here: function updateRecords(records, id, prop, value)
and the function call: updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);
Those are so called “positional arguments” meaning whatever is writting at the position in the function call, is treated as the name written in the function definition.

  1. recordCollection → records
  2. 5439 → id
  3. ‘artist’ → prop
  4. ‘ABBA’ → value

Ok I think I get it.
In order to access prop, you first have to access the object and find the right id to get to prop?

record[id] is the album, record[id][prop] is the album’s prop

1 Like