I have not looked at your logic, so you may have other stuff to fix later but…
when the function has paramaters,
you need to use those instead of referencing global objects that are passed in anyway - your function is not reusable
otherwise
So in the bit where prop=='tracks', you have two branches - one where the record doesn’t have a tracks property and one where value!=="". The second term on those is causing you problems.
Think about when you want to add something to the tracks property. There’s only one time you do, related to the value.
So rather than one if after another, perhaps one inside the other. So you know you’ll be writing to the tracks property whether or not it exists. Inside that first if, perhaps, is a good place to check if there is a tracks?
I tried everything! I plugged in every possible solution I can find on the web, nothing works. What gives??? Can someone please help step by step in plain English with no jargons?
Your code so far
// 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'
}
};
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (!value) {
delete collection[id][prop];
return collection;
}
if(prop !== "tracks" && value) {
collection[id][prop] = value;
} else { if (!collection[id].hasOwnProperty("tracks")) collection[id].tracks = [];
collection[id].tracks.push(value);
}
return collection;
}
updateRecords(collection, 5439, 'artist', 'ABBA');
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.