I am having some trouble with the JavaScript Algorithms and Data Structures problem titled “Record Collection” (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection). I see other posts on here relating to this challenge, but none look like the same issue I am having.
My Code is:
// Setup
var 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 (value) {
switch (prop) {
case "tracks":
if (recordCollection[id].hasOwnProperty(prop)) {
recordCollection[id][prop].push(value);
} else {
recordCollection[id][prop] = [value];
}
break;
default:
recordCollection[id][prop] = value;
break;
}
} else {
delete recordCollection[id][prop];
}
console.log(recordCollection);
return recordCollection;
}
updateRecords(recordCollection, 2548, "artist", "");
The two parts failing are:
- After
updateRecords(recordCollection, 2548, "artist", "")
,artist
should not be set - After
updateRecords(recordCollection, 2548, "tracks", "")
,tracks
should not be set
When I try to run these two, my output looks correct to me. For instance, in the code I posted above my result for id 2548 doesn’t even show ‘artist’.
Here is the output from my code above:
{ '1245': { artist: 'Robert Palmer', tracks: [] },
'2468':
{ albumTitle: '1999',
artist: 'Prince',
tracks: [ '1999', 'Little Red Corvette' ] },
'2548':
{ albumTitle: 'Slippery When Wet',
tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
'5439': { albumTitle: 'ABBA Gold' } }
Thanks in advance
Edits: First edit was to remove a word. Second edit was to show output