Record Collection - Don't understand the solution

Hi everyone,

I’ve tried freecodecamp on and off for a few years, and have always struggled with what many would consider to be the basic logic of programming languages. In particular, I’ve got to these Javascript lessons in the past and, as soon as they involve recalling information from previous lessons, I’ve lost track of the entire process!

Anyhow, I was hoping for a more detailed explanation of the solution to the Record Collection lesson. I sort of get what’s happening, but not entirely. I gather that the script is attempting to assign properties to the ‘value’ function (or variable?), but I don’t really understand how the script is determining what is ‘id’ or ‘prop’ from the collection variable.

You can post your code and try to explain what you think it’s doing step by step. It’s a good exercise and will really help your understanding!

If you have any questions or make any mistakes in your explanation, we can help you here. :slight_smile:

Hello!

Have you clicked the “Get a Hint” button? This usually has a helpful explanation of the challenge.

If you did click that, and still don’t understand, I can try to explain real quick.

There are 3 parts to the code: the collection object, the declaration of the updateRecords function, and the calling of the updateRecords function.

The collection object is the ‘records’ that you will be updating. It is a collection of musical albums, each of which has a number (ex. 1042) for an id, an album name (ex. album: “Slippery when wet”), and artist, and an array of tracks that are on the album.

Next, we have a function being declared called updateRecords that looks like this:

updateRecords(id, prop, value){
    //your code
}

When we call the function later, we will need to pass in 3 parameters: id, prop (short for property), and value. The challenge has a list of rules that we must follow, but essentially, we want to be able to call the function like this:

updateRecords(5439, "artist", "ABBA");

and it will

  1. Check if there is an album with the given id (5439)
  2. Create, edit, or delete the given property (“artist”) with the given value (“ABBA”)
  3. Return the whole, updated collection
1 Like

Thanks @lucassorenson, this is an extremely helpful summary of what’s happening! I’d tried the hint (and solution too) but I didn’t really follow the process it laid out, so I’m grateful for your explanation.

I have a follow-up question though - how does the updateRecords function know that the id is the numbers listed in the first row of the collections object? I’ll use the code in the lesson to try and explain myself better:

updateRecords(5439, “artist”, “ABBA”);

Is the updateRecords function looking at these three numbers/strings and determining that, as 5439 is the first number/string listed, it must be id, the second prop, etc.?

Unfortunately when I test the correct code it doesn’t actually show anything in the console - it just says the following and lets me move onto the next exercise. It means I can’t really determine what the code is doing to the collections object…

// running tests
// tests completed

That’s what is passed to the function as arguments.

When you declare a function, you can specify how many parameters accept, and how the function interact with that.

function example(a, b, c, d) {
  // here you can interact with them as a, b , c...
}

The interpreter see this definition and “keeps it for later” when you will invoke it.

Once invoked, you usually pass some arguments to the function, those are the actual value that the function will use:

example(1, 'test', 4, 'umbrella')
// a -> 1, b -> 'test', c -> 4, d -> 'umbrella'

example(2)
// a -> 2,  b ,c ,d -> undefined

So, as you stated

That is correct because this is the order of the parameters that you defined.


Some languages also forces you to define the type those parameters needs to be, otherwise it will throw an error

function add(a: number, b:number): number {
 return a + b;
}

add(1, 'test') // Error. type b number expected found string

Hope this helps.

1 Like

Just to bring @Marmiz’s explanation full circle, your updateRecords function looks like this:

updateRecords(id, prop, value){
   //your code
}

So when you call it, you will include 3 parameters: id, prop, and value. If you look at the function declaration, id is the first parameter, followed by prop, then value. That is the order that these props need to go in.

If you defined the function as

updateRecords(prop, value, id){
    //your code
}

Then all of your code would be the same, but when the function is called, it would have to be switched around like so

updateRecords ("artist", "ABBA", 5439)

So the computer knows which parameter is which based on the order we give them to the function. We have to supply the parameters to the function in the same order that we defined them.

I hope this clears it up for you, let me know.

1 Like

@lucassorenson and @Marmiz, thank you both very much for taking the time to answer my questions. This will hopefully act as a useful reference point for future Javascript lessons, as you’ve explained not only how to solve the problem, but why the various elements of the code act the way they do.

I’m extremely grateful for your help!