Record Collection I don't understand

This code is chaotic and impossible for me to comprehend. Can anyone explain to me what it is doing?


const recordCollection = {

  2548: {

    albumTitle: 'Slippery When Wet',

    artist: 'Bon Jovi',

    tracks: ['Let It Rock', 'You Give Love a Bad Name']

  },

  2468: {

    albumTitle: '1999',

    artist: 'Prince',

    tracks: ['1999', 'Little Red Corvette']

  },

  1245: {

    artist: 'Robert Palmer',

    tracks: []

  },

  5439: {

    albumTitle: 'ABBA Gold'

  }

};

// Only change code below this line

function updateRecords(records, id, prop, value) {

  if (prop !== "tracks" && value !== "") {

    records[id][prop] = value;

  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {

    records[id][prop] = [value];

  } else if (prop === "tracks" && value !== "") {

    records[id][prop].push(value);

  } else if (value === "") {

    delete records[id][prop];

  }

  return records;

}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');```

What part specifically do you not understand. I wouldn’t really describe it as ‘chaotic’ as it has an exact, predictable, deterministic result from the logic of the function.

First you have an object, which represents the record collection. Inside it, the collection is structured with numbers(2548, 2468, 1245 and so on; each representing an object key). Every number represents an album and is also, in the form of an object, having its own properties(keys), like “artist”, “albumTitle” and “tracks” and each one has its approrpiate value, be it the name ofthe artist, the name of the album, or an array including track names.

Then you have the updateRecords function. It takes four arguments. The first needs to be a record collection object, the one its supposed to “update”. It must have a similar structure like the “recordCollection” object you have on top of the code. The second is an “id”, which points to the album id that should be updated. Remember how the recordColelction object had its albums tagged with ID’s. The 3rd argument is a property for the said album, just like albums within the recordCollection have such(artist, tracks, albumTitle etc.). And finally the 4th argument is the value which the property passed(3rd argument) should have.
The challenge has detailed explanation on how the function should operate, how it should use the said arguments, in order to fulfil some use and i wont go into details about that.

The last line of the code calls the function “updateRecords”, providing the required 4 arguments, a record collection, an album ID, an album property and its value.

If you have blank spots regarding the conditionas within the function body, be more specific and i, or someone else will clear it out. It basically modify the passed records object, accodring the challenge requirements and then return's the updated object

This lesson just made me lose any hope in learning javaScript, can somebody explain to me like I’m a retard where did “records, id, prop, value” come from and how are they conected to albumTitle, artist, and tracks

How are const recordCollection and function updateRecords even conected. How does the function know id is 2548 and records are tracks

To start at the beginning, we call this function

updateRecords(recordCollection, 5439, 'artist', 'ABBA');

We are passing the function four parameters:

  • The recordCollection, which is a reference to an object that contains all our records. Within updateRecords, that will be assigned to the records variable.
  • the number 5439, which is a reference to a particular record within that collection. As the collection is an object, that’s the property name within that object. Within updateRecords, we’ll assign that to the variable id.
  • The string 'artist', which is a reference to a property within the record we want to add, edit or delete. Inside updateRecords, it’ll be called prop.
  • And the string 'ABBA', which is the value we want to use to update that particular property. Inside updateRecords, this one will be value.

This is the order arguments go into the function, as its been defined:

function updateRecords(records, id, prop, value){ 
 /* function body */
}

So we know we’ll have to pass in that records object (our function doesn’t know about anything outside of itself, so we have to tell it “Hey, here’s the records!”).

That records is a reference to the recordCollection object. If we look at it, we can see that it’s just a normal Object literal:

const recordCollection: {
  2548: { /* sub-object */ },
  2468: { /* sub-object */ },
  1245: { /* sub-object */ },
  5439: { /* sub-object */ }
}

It’s an object with some number of things inside it. Each of those things is a record, identified by an id. We pass in the id of the one we want (in this case, 5439), and we can use that particular id to find the property we want. By using the records variable in our function as a reference to the collection, and the id variable as the particular one we want, records[id] will give us records[5439]. That looks like:

5439: {
  albumTitle: 'ABBA Gold'
}

So we have the particular record we want, the id was useful for that, but what do we want to do to it? The spec tells us we want to do something with a particular property. Which property? The one passed to our function in the prop argument (in this case, "artist").

The function knows only what we tell it. We’re provided the recordCollection so we can see the “shape” of the data, by which I mean we can see what a record should contain. There are some things we can tell, from looking at both the data itself and from looking at the spec:

  • Given a particular record (records[id]), we want to find the particular property on that object and do something with it.
  • If we don’t have a value passed in, we are removing that particular property.
  • Otherwise, in all cases except for the tracks property, we want to simply set the property to the given value.
  • tracks is the only odd property out. That one contains an array. If we’re adding a value to tracks, we need to be sure there’s an array there for us to add to before we add anything to it.

So the spec tells us what the “shape” of the data is, and what steps we want to take. So we start writing the updateRecords function, knowing it’ll be passed some sort of object (which we’ll call records) that follows a given structure. The structure is that we see in recordCollection, but might or might not BE recordCollection. We aren’t tied to that, as we’ll be passing something in.

So we know that records will be an object, and it will contain one or more nested objects within it. And we know that, if we want to access the tracks property in that nested object, we have to treat that and only that as an array. Any other property will simply be deleted, created or overwritten, but tracks will either be deleted or added to. That one is unique. Can’t stress that enough.

This was a long semi-lecture, but the point is this: recordCollection and updateRecords are only connected because recordCollection is being passed into updateRecords as its first parameter.

2 Likes

Thank you for your time and patience, this lesson was very confusing at first because everything is worded similar .

I didn’t understand when calling the function with
updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);

that we are saying
recordColection=records,
5439 = id,
‘artist’ = prop,
‘ABBA’= value

That’s basically user input. The stuff we want to enter to the function that is missing. (If i understand it now correctly)

I was confusing artist: with ‘artist’
and 5439: with 5439,

from the updateRecords(recordCollection, 5439, ‘artist’, ‘ABBA’);

1 Like

Within the function, we define parameters. Those are the variables that we will use to reference the data our function will receive. We don’t do anything special, simply place them in the parentheses while we define the function. The parameters are variables.

Later, when we or others use our function, data gets passed in as arguments. From the outside, it might be data in some other variable or defined in place or returned from some other function, but the values being fed in are simple data. From outside, nothing needs know of the parameter names, as they’re only internal (private) references.

So when you hear arguments in reference to a function, think “the data being fed in.” When you hear parameters, think “three variable name referencing some passed-in data.”

1 Like