I'm a newbie so please don't be harsh on me.
Here's the object and the link to it:
// 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'
}
};
Here's the solution:
function updateRecords(records, id, prop, value) {
// Access target album in record collection
const album = records[id];
// If value is an empty string,
// delete the given prop property from the album
if (value === "") {
delete album[prop];
}
// If prop isn't tracks,
// update or set that album's prop to value
else if (prop !== "tracks") {
album[prop] = value;
}
// If prop is tracks,
// add value to the end of the album's existing tracks array
else {
album["tracks"] = album["tracks"] || [];
album["tracks"].push(value);
}
// Your function must always return the entire record collection object
return records;
}
I passed the challenge and then tried to do more and check - if the given value exists then do nothing. I tried to do it with hasOwnProperty() and indexOf(), but I always get a TypeError:
TypeError: Cannot read properties of undefined (reading 'indexOf')
While googling the correct way of doing it, i only found even bigger functions just to check if the given value exists. Could you help me, please? How would you do it?
if (album[prop] === value) // do nothing?