Javascript "Record Collection": Need a deep and simple explanation please!

Javascript "Record Collection": Need a deep and simple explanation please!


So I’m new to Javascript and so far everything is dandy until the “Record Collection” part of Basic Javascript in FreeCodeCamp. I thought I understood what the instruction for the particular exercise was but after two hours of toiling with it, I caved and went for the answer and the answer was not what I expected it so what I am asking is:

What was the purpose of the exercise?
Here’s the exercise instruction:

Write a function which takes an album’s id (like 2548), a property prop (like “artist” or “tracks”), and a value (like “Addicted to Love”) to modify the data in this collection.

If prop isn’t “tracks” and value isn’t empty (""), update or set the value for that record album’s property.

Your function must always return the entire collection object.

There are several rules for handling incomplete data:

If prop is “tracks” but the album doesn’t have a “tracks” property, create an empty array before adding the new value to the album’s corresponding property.

If prop is “tracks” and value isn’t empty (""), push the value onto the end of the album’s existing tracks array.

If value is empty (""), delete the given prop property from the album."

And what does each line of the answer function mean?

Here’s the code:

var collection = {
    "2548": {
      "album": "Slippery When Wet",
      "artist": "Bon Jovi",
      "tracks": [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
    "2468": {
      "album": "1999",
      "artist": "Prince",
      "tracks": [ 
        "Little Red Corvette" 
    "1245": {
      "artist": "Robert Palmer",
      "tracks": [ ]
    "5439": {
      "album": "ABBA Gold"

var collectionCopy = JSON.parse(JSON.stringify(collection));

**and this was the answer:**

function updateRecords(id, prop, value) {
  if (prop === "tracks" && value !== "") {
   if(collection[id][prop]) {
   else {
  } else if (value !== "") {
    collection[id][prop] = value;
  } else {
    delete collection[id][prop];

  return collection;

Thank you in advance for taking your time out to help a newbie!


I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


I am having the same problem! Did you ever figure it out?