Skip to content

Instantly share code, notes, and snippets.

@abhisekp
Last active April 26, 2017 04:07
Show Gist options
  • Save abhisekp/90c21b8f8af30ce3fdec6340ec954aaf to your computer and use it in GitHub Desktop.
Save abhisekp/90c21b8f8af30ce3fdec6340ec954aaf to your computer and use it in GitHub Desktop.
FreeCodeCamp Challenge Explanations

Record Collection Help Instruction

Read the instruction 2-3 times and the gist of it is

  • a function receives 3 parameters i.e. id, prop and value
  • get the collection with the given idfrom the given collections and store it in a variable
  • if value is empty string or undefined, then delete the corresponding property prop of the collection
  • if prop is 'tracks', then check if tracks property is present in the collection and then add the value to the tracks array. Otherwise, create the array and add the value as its 1st element
  • if prop is not 'tracks', then simply create the property and assign the value 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 or undefined
  • and if it so, then simply delete the prop from the collection and return the collection

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