— by Abhisek Pattnaik <[email protected]>
Last active
April 26, 2017 04:07
-
-
Save abhisekp/90c21b8f8af30ce3fdec6340ec954aaf to your computer and use it in GitHub Desktop.
FreeCodeCamp Challenge Explanations
Read the instruction 2-3 times and the gist of it is
- a function receives 3 parameters i.e.
id
,prop
andvalue
- get the collection with the given
id
from the given collections and store it in a variable - if
value
is empty string orundefined
, then delete the corresponding propertyprop
of the collection - if
prop
is'tracks'
, then check iftracks
property is present in the collection and then add thevalue
to thetracks
array. Otherwise, create the array and add thevalue
as its 1st element - if
prop
is not'tracks'
, then simply create the property and assign thevalue
to it.
This is the overall summary
if u read the instruction some N number of times, then u find that when no value
is given, then we simply delete the prop
and return the collection
. And we don't need to calculate or process anything else.
So the 1st thing is to
- get the collection corresponding to the given collection
id
- check if
value
is either empty string orundefined
- and if it so, then simply
delete
theprop
from the collection and return thecollection
use temporary variables. E.g.
const currCollection = collection[id]
if (prop === 'tracks') {
if (currCollection[prop]) // check if property is present
currCollection[prop].push(value)
else
// create a new array and assign the `value` as 1st element
currCollection[prop] = [value]
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment