Hello, fellow programmers. I have been learning Javascript on Freecodecamp for quite some time now and so far I am at 82%. I got really stuck at this level and I don’t know how to pass the test. Please, help me with the solution to the code below:
Record Collection
You are given an object literal representing a part of your musical album collection. Each album has a unique id number as its key and several other properties. Not all albums have complete information.
You start with an updateRecords
function that takes an object literal, records
, containing the musical album collection, an id
, a prop
(like artist
or tracks
), and a value
. Complete the function using the rules below to modify the object passed to the function.
- Your function must always return the entire record collection object.
- If
prop
isn’ttracks
andvalue
isn’t an empty string, update or set that album’sprop
tovalue
. - If
prop
istracks
but the album doesn’t have atracks
property, create an empty array and addvalue
to it. - If
prop
istracks
andvalue
isn’t an empty string, addvalue
to the end of the album’s existingtracks
array. - If
value
is an empty string, delete the givenprop
property from the album.
Note: A copy of the recordCollection
object is used for the tests.
// 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 (records) {
return records[recordCollection];
}
else {
return records;
}
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36
Challenge: Record Collection
Link to the challenge: