I am completing the freecodecamp exercises and am wondering what the difference is between my two attempted solutions. The first solution is accepted but the second is rejected. I wanted to try using dot notation instead of bracket notation, but something must be wrong.
// 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'
}
};
Exercise: Complete the function using the rules below to modify the object passed to the function. 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. If prop is tracks but the album doesn't have a tracks property, create an empty array and add value to it. If prop is tracks and value isn't an empty string, add value to the end of the album's existing tracks array. If value is an empty string, delete the given prop property from the album.
// Solution 1 using bracket notation (accepted)
function updateRecords(records, id, prop, value) {
if (prop != 'tracks' && value != '') {
records[id][prop] = value;
} else if (prop == 'tracks' && records[id].hasOwnProperty(prop) == false) {
records[id][prop] = [value];
} else if (prop == 'tracks' && value != '') {
records[id][prop].push(value);
} else if (value == '') {
delete records[id][prop];
}
return records;
}
// Solution 2 using dot notation (rejected)
function updateRecords(records, id, prop, value) {
if (prop != 'tracks' && value != '') {
records.id.prop = value;
} else if (prop == 'tracks' && records.id.hasOwnProperty(prop) == 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');