I stuck on one of the test which is
After
updateRecords(collection, 2468, "tracks", "Free")
,tracks
should have1999
as the first element.
And I couldn’t figure it out what was wrong with my code
Here is my code:
function updateRecords(object, id, prop, value) {
if (prop != 'tracks' && value != ''){
object[id][prop] = value;
}
if (prop == 'tracks' && object.hasOwnProperty('tracks') == false){
object[id][prop] = [value];
}
if (prop == 'tracks' && value != ''){
object[id][prop].push(value);
}
if (value == ''){
delete object[id][prop];
}
return object;
}
console.log(updateRecords(collection, 2468, 'tracks', 'Free'));
and this is the output (Notice: the [“Free”, " Free"]
{ '1245': { artist: 'Robert Palmer', tracks: [] },
'2468':
{ albumTitle: '1999',
artist: 'Prince',
tracks: [ 'Free', 'Free' ] },
'2548':
{ albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: [ 'Let It Rock', 'You Give Love a Bad Name' ] },
'5439': { albumTitle: 'ABBA Gold' } }
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 Edg/87.0.664.66
.
Challenge: Record Collection
Link to the challenge: