— by Abhisek Pattnaik <abhisekp@engineer.com>
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,propandvalue - get the collection with the given
idfrom the given collections and store it in a variable - if
valueis empty string orundefined, then delete the corresponding propertypropof the collection - if
propis'tracks', then check iftracksproperty is present in the collection and then add thevalueto thetracksarray. Otherwise, create the array and add thevalueas its 1st element - if
propis not'tracks', then simply create the property and assign thevalueto 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
valueis either empty string orundefined - and if it so, then simply
deletethepropfrom 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
