Hey there,
My code seems to be working in regards to adding ‘ABBA’ to the artist, getting rid of the ‘Bon Jovi’ as an artist, adding ‘Free’ as a track to Prince, adding ‘Addicted to Love’ as a track for Robert Palmer and the album ‘Riptide’, but it is not making the tracks for ‘Slippery When Wet’ (id 2548) have an empty array. It is working as far as I can tell, except creating a “tracks” property on the end of 2548… Which is confusing because it is creating a tracks property with a value for ABBA - 5439 (when you run it). The empty brackets on the other side of || should create the property tracks but with an empty array if there are no track values passed through. Any insight into why this is happening would be much appreciated.
// Setup
var collection = {
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(object, id, prop, value) {
if (value === "") {
delete collection[id][prop];
} else if (prop === "tracks") {
collection[id][prop] = collection[id][prop] || [];
collection[id][prop].push(value);
} else {
collection[id][prop] = value
}
return object;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
updateRecords(collection, 5439, "tracks", "Take A Chance On Me");
updateRecords(collection, 2548, "artist", "");
updateRecords(collection, 1245, "tracks", "Addicted To Love");
updateRecords(collection, 2468, "tracks", "Free");
updateRecords(collection, 2548, "tracks", "");
console.log(updateRecords(collection, 1245, "albumTitle", "RipTide"))
Here is the output when I run the tests;
// running tests
After updateRecords(collection, 5439, "artist", "ABBA"), artist should be the string ABBA
After updateRecords(collection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last element.
After updateRecords(collection, 2548, "artist", ""), artist should not be set
After updateRecords(collection, 1245, "tracks", "Addicted to Love"), tracks should have the string Addicted to Love as the last element.
After updateRecords(collection, 2548, "tracks", ""), tracks should not be set
{ '1245':
{ artist: 'Robert Palmer',
tracks: [ 'Addicted To Love' ],
albumTitle: 'RipTide' },
'2468':
{ albumTitle: '1999',
artist: 'Prince',
tracks: [ '1999', 'Little Red Corvette', 'Free' ] },
'2548': { albumTitle: 'Slippery When Wet' },
'5439':
{ albumTitle: 'ABBA Gold',
artist: 'ABBA',
tracks: [ 'Take A Chance On Me' ] } }
After updateRecords(collection, 1245, "albumTitle", "Riptide"), albumTitle should be the string Riptide
// tests completed
// console output
{ '1245':
{ artist: 'Robert Palmer',
tracks: [ 'Addicted To Love' ],
albumTitle: 'RipTide' },
'2468':
{ albumTitle: '1999',
artist: 'Prince',
tracks: [ '1999', 'Little Red Corvette', 'Free' ] },
'2548': { albumTitle: 'Slippery When Wet' },
'5439':
{ albumTitle: 'ABBA Gold',
artist: 'ABBA',
tracks: [ 'Take A Chance On Me' ] } }
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0
.
Challenge: Record Collection
Link to the challenge: