|
| 1 | +--- |
| 2 | +id: 56533eb9ac21ba0edf2244cf |
| 3 | +title: Build a Record Collection |
| 4 | +challengeType: 26 |
| 5 | +dashedName: build-a-record-collection |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +The `updateRecords` function takes 4 arguments represented by the following function parameters: |
| 13 | + |
| 14 | +- `records` - an object containing several individual albums |
| 15 | +- `id` - a number representing a specific album in the `records` object |
| 16 | +- `prop` - a string representing the name of the album’s property to update |
| 17 | +- `value` - a string containing the information used to update the album’s property |
| 18 | + |
| 19 | +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. |
| 20 | + |
| 21 | +**User Stories:** |
| 22 | + |
| 23 | +1. Your function must always return the entire `records` object. |
| 24 | +2. If `value` is an empty string, delete the given `prop` property from the album. |
| 25 | +3. If `prop` isn't `tracks` and `value` isn't an empty string, assign the `value` to that album's `prop`. |
| 26 | +4. If `prop` is `tracks` and `value` isn't an empty string, but the album doesn't have a `tracks` property, create an empty array and add `value` to it. |
| 27 | +5. If prop is `tracks` and `value` isn't an empty string, add `value` to the end of the album's existing `tracks` array. |
| 28 | + |
| 29 | +**Note:** A copy of the `recordCollection` object is used for the tests. Your function should not directly refer to the `recordCollection` object, only the function parameter. |
| 30 | + |
| 31 | +# --before-each-- |
| 32 | + |
| 33 | +```js |
| 34 | +const _recordCollection = { |
| 35 | + 2548: { |
| 36 | + albumTitle: 'Slippery When Wet', |
| 37 | + artist: 'Bon Jovi', |
| 38 | + tracks: ['Let It Rock', 'You Give Love a Bad Name'] |
| 39 | + }, |
| 40 | + 2468: { |
| 41 | + albumTitle: '1999', |
| 42 | + artist: 'Prince', |
| 43 | + tracks: ['1999', 'Little Red Corvette'] |
| 44 | + }, |
| 45 | + 1245: { |
| 46 | + artist: 'Robert Palmer', |
| 47 | + tracks: [] |
| 48 | + }, |
| 49 | + 5439: { |
| 50 | + albumTitle: 'ABBA Gold' |
| 51 | + } |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +# --hints-- |
| 56 | + |
| 57 | +You should have a `updateRecords` function. |
| 58 | + |
| 59 | +```js |
| 60 | +assert.isFunction(updateRecords); |
| 61 | +``` |
| 62 | + |
| 63 | +After `updateRecords(recordCollection, 5439, "artist", "ABBA")`, `artist` should be the string `ABBA` |
| 64 | + |
| 65 | +```js |
| 66 | +assert.equal( |
| 67 | + updateRecords(_recordCollection, 5439, 'artist', 'ABBA')[5439]['artist'], |
| 68 | + 'ABBA' |
| 69 | +); |
| 70 | +``` |
| 71 | + |
| 72 | +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. |
| 73 | + |
| 74 | +```js |
| 75 | +updateRecords(_recordCollection, 5439, 'tracks', 'Take a Chance on Me'); |
| 76 | +assert.lengthOf(_recordCollection[5439]['tracks'], 1); |
| 77 | +assert.equal(_recordCollection[5439]['tracks'].pop(), 'Take a Chance on Me'); |
| 78 | +``` |
| 79 | + |
| 80 | +After `updateRecords(recordCollection, 2548, "artist", "")`, `artist` should not be set |
| 81 | + |
| 82 | +```js |
| 83 | +updateRecords(_recordCollection, 2548, 'artist', ''); |
| 84 | +assert.isFalse(_recordCollection[2548].hasOwnProperty('artist')); |
| 85 | +``` |
| 86 | + |
| 87 | +After `updateRecords(recordCollection, 1245, "tracks", "Addicted to Love")`, `tracks` should have the string `Addicted to Love` as the last element. |
| 88 | + |
| 89 | +```js |
| 90 | +assert.equal( |
| 91 | + updateRecords(_recordCollection, 1245, 'tracks', 'Addicted to Love')[1245][ |
| 92 | + 'tracks' |
| 93 | + ].pop(), 'Addicted to Love' |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +After `updateRecords(recordCollection, 2468, "tracks", "Free")`, `tracks` should have the string `1999` as the first element. |
| 98 | + |
| 99 | +```js |
| 100 | +assert.equal( |
| 101 | + updateRecords(_recordCollection, 2468, 'tracks', 'Free')[2468][ |
| 102 | + 'tracks' |
| 103 | + ][0], '1999' |
| 104 | +); |
| 105 | +``` |
| 106 | + |
| 107 | +After `updateRecords(recordCollection, 2548, "tracks", "")`, `tracks` should not be set |
| 108 | + |
| 109 | +```js |
| 110 | +updateRecords(_recordCollection, 2548, 'tracks', ''); |
| 111 | +assert.isFalse(_recordCollection[2548].hasOwnProperty('tracks')); |
| 112 | +``` |
| 113 | + |
| 114 | +After `updateRecords(recordCollection, 1245, "albumTitle", "Riptide")`, `albumTitle` should be the string `Riptide` |
| 115 | + |
| 116 | +```js |
| 117 | +assert.equal( |
| 118 | + updateRecords(_recordCollection, 1245, 'albumTitle', 'Riptide')[1245][ |
| 119 | + 'albumTitle' |
| 120 | + ], 'Riptide' |
| 121 | +); |
| 122 | +``` |
| 123 | + |
| 124 | +# --seed-- |
| 125 | + |
| 126 | +## --seed-contents-- |
| 127 | + |
| 128 | +```js |
| 129 | +const recordCollection = { |
| 130 | + 2548: { |
| 131 | + albumTitle: 'Slippery When Wet', |
| 132 | + artist: 'Bon Jovi', |
| 133 | + tracks: ['Let It Rock', 'You Give Love a Bad Name'] |
| 134 | + }, |
| 135 | + 2468: { |
| 136 | + albumTitle: '1999', |
| 137 | + artist: 'Prince', |
| 138 | + tracks: ['1999', 'Little Red Corvette'] |
| 139 | + }, |
| 140 | + 1245: { |
| 141 | + artist: 'Robert Palmer', |
| 142 | + tracks: [] |
| 143 | + }, |
| 144 | + 5439: { |
| 145 | + albumTitle: 'ABBA Gold' |
| 146 | + } |
| 147 | +}; |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | +# --solutions-- |
| 154 | + |
| 155 | +```js |
| 156 | +const recordCollection = { |
| 157 | + 2548: { |
| 158 | + albumTitle: 'Slippery When Wet', |
| 159 | + artist: 'Bon Jovi', |
| 160 | + tracks: ['Let It Rock', 'You Give Love a Bad Name'] |
| 161 | + }, |
| 162 | + 2468: { |
| 163 | + albumTitle: '1999', |
| 164 | + artist: 'Prince', |
| 165 | + tracks: ['1999', 'Little Red Corvette'] |
| 166 | + }, |
| 167 | + 1245: { |
| 168 | + artist: 'Robert Palmer', |
| 169 | + tracks: [] |
| 170 | + }, |
| 171 | + 5439: { |
| 172 | + albumTitle: 'ABBA Gold' |
| 173 | + } |
| 174 | +}; |
| 175 | + |
| 176 | +function updateRecords(records, id, prop, value) { |
| 177 | + if (value === '') delete records[id][prop]; |
| 178 | + else if (prop === 'tracks') { |
| 179 | + records[id][prop] = records[id][prop] || []; |
| 180 | + records[id][prop].push(value); |
| 181 | + } else { |
| 182 | + records[id][prop] = value; |
| 183 | + } |
| 184 | + |
| 185 | + return records; |
| 186 | +} |
| 187 | +``` |
0 commit comments