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.
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
Check if there is an album with the given id (5439)
Create, edit, or delete the given property (“artist”) with the given value (“ABBA”)
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…
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.
@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.