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 therecordCollection
object is used for the tests.
Hello @u19192372
It's a lot of logical reasoning.
if
checks if there is anyvalue
being passed as a function parameter. If not, we delete theprop
that was passed;if
checks if the third parameter istracks
. Here we already know that somevalue
has been passed, as it has already passed the firstif
. Then:tracks
array already exists. If so, he moves on. If it doesn't exist, it creates it as an empty array;value
passed into this array;value
was passed and ifprop
is nottracks
) I just update thevalue
of theprop
.