5.1 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 56533eb9ac21ba0edf2244cf | Record Collection | 1 | 18261 | record-collection |
--description--
You are creating a function that aids in the maintenance of a musical album collection. The collection is organized as an object that contains multiple albums which are also objects. Each album is represented in the collection with a unique id as the property name. Within each album object, there are various properties describing information about the album. Not all albums have complete information.
The updateRecords function takes 4 arguments represented by the following function parameters:
records- an object containing several individual albumsid- a number representing a specific album in therecordsobjectprop- a string representing the name of the album’s property to updatevalue- a string containing the information used to update the album’s property
Complete the function using the rules below to modify the object passed to the function.
- Your function must always return the entire
recordsobject. - If
valueis an empty string, delete the givenpropproperty from the album. - If
propisn'ttracksandvalueisn't an empty string, assign thevalueto that album'sprop. - If
propistracksandvalueisn't an empty string, but the album doesn't have atracksproperty, create an empty array and addvalueto it. - If prop is
tracksandvalueisn't an empty string, addvalueto the end of the album's existingtracksarray.
Note: A copy of the recordCollection object is used for the tests. You should not directly modify the recordCollection object.
--before-each--
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'
}
};
--hints--
After updateRecords(recordCollection, 5439, "artist", "ABBA"), artist should be the string ABBA
assert(
updateRecords(_recordCollection, 5439, 'artist', 'ABBA')[5439]['artist'] ===
'ABBA'
);
After updateRecords(recordCollection, 5439, "tracks", "Take a Chance on Me"), tracks should have the string Take a Chance on Me as the last and only element.
assert(
updateRecords(_recordCollection, 5439, 'tracks', 'Take a Chance on Me') &&
_recordCollection[5439]['tracks'].length === 1 &&
_recordCollection[5439]['tracks'].pop() === 'Take a Chance on Me'
);
After updateRecords(recordCollection, 2548, "artist", ""), artist should not be set
updateRecords(_recordCollection, 2548, 'artist', '');
assert(!_recordCollection[2548].hasOwnProperty('artist'));
After updateRecords(recordCollection, 1245, "tracks", "Addicted to Love"), tracks should have the string Addicted to Love as the last element.
assert(
updateRecords(_recordCollection, 1245, 'tracks', 'Addicted to Love')[1245][
'tracks'
].pop() === 'Addicted to Love'
);
After updateRecords(recordCollection, 2468, "tracks", "Free"), tracks should have the string 1999 as the first element.
assert(
updateRecords(_recordCollection, 2468, 'tracks', 'Free')[2468][
'tracks'
][0] === '1999'
);
After updateRecords(recordCollection, 2548, "tracks", ""), tracks should not be set
updateRecords(_recordCollection, 2548, 'tracks', '');
assert(!_recordCollection[2548].hasOwnProperty('tracks'));
After updateRecords(recordCollection, 1245, "albumTitle", "Riptide"), albumTitle should be the string Riptide
assert(
updateRecords(_recordCollection, 1245, 'albumTitle', 'Riptide')[1245][
'albumTitle'
] === 'Riptide'
);
--seed--
--seed-contents--
// 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'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
--solutions--
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'
}
};
// Only change code below this line
function updateRecords(records, id, prop, value) {
if (value === '') delete records[id][prop];
else if (prop === 'tracks') {
records[id][prop] = records[id][prop] || [];
records[id][prop].push(value);
} else {
records[id][prop] = value;
}
return records;
}