Basic JavaScript - Record Collection

Tell us what’s happening:

I’ve looked at some of the solutions provided for this example and I’m hoping someone can direct me to an explanation how the “id” argument in the updateRecords function is automatically assigned to the recordCollection number id in the Object?

I don’t understand how “id” doesn’t need to be defined anywhere, yet the ‘id’ argument in the function is used to lookup the key value within the object?

Any insights would be appreciated (if the above makes sense).

is automatically recognised as the

  **Your code so far**
// Setup
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) {
return records;
}

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

This from the challenge:

// Only change code below this line
function updateRecords(records, id, prop, value) {
  return records;
}

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

Note the the value 5439, the second argument in the function refers to as the id. And, note the field:

const recordCollection = {
  2548: {
    albumTitle: 'Slippery When Wet',
    ...

The id value is the property name for each album object - 2548 in the above. The id is not the actual property name, but only used in the function as a parameter needs to be identified by a name (and in this case it is id). One could have used another parameter name as album_id, etc., for example: function updateRecords(records, album_id, prop, value) {...

You learn about how that works in this lesson https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables

const person = {
  name: 'Colin'
};

const key = 'name';

// all three of the following lines evaluate to 'Colin'
person.name; // dot notation with the literal property name
person['name']; // bracket notation with a string literal
person[key]; // bracket notation with a variable

Thanks so much for the response @prasadsaya .
I’m not sure what I’m missing in my understanding here.
So far in the course, we’ve accessed property values within objects using the property name, or its position within arrays.
In this case, shouldn’t the actual number id’s in the object, be used as the arguments in the function?
I guess I’m still not clear in this example, why/how the function parameter ‘id’ (or any other variable name) is specifically linked/assigned/mapped to the number id within the object.
If you know any resources that could help me piece this together?
thanks again

thanks @colinthornton
Yep, agree. In that lesson though, the property was assigned to a variable:
const myDog = “Hunter”;
My struggle in this particular challenge, is that the “id” argument isn’t assigned/mapped to anything? it’s just added as the second argument, and how do we just assume it will be linked to the object property?
Cheers

It’s the second parameter to the function, so when you call the function, you pass in its value. You can see that on the final line of the code on this challenge.

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

In this example function call, the recordCollection variable declared above the function is passed in as the records argument, 5439 is passed in as the id argument, 'artist' is passed in as the prop argument, and 'ABBA' is passed in as the value argument.

thanks again @colinthornton
Here’s what I’m trying to say I guess:
If for example, we console.log(recordCollection.id)
it returns undefined on the console, i assume because a parameter named id doesn’t exist in the Object… So how is the “id” argument in the function recognised as the id in the Object?
I’m so sorry - this just has me stumped!

Look at the properties on the records object. It has 4, and those keys are:

  • 2548
  • 2468
  • 1245
  • 5439

Notice there’s no “id” property, so naturally records.id is undefined.

If you want a defined value you’d have to look up via one of the keys I listed above.

  • records[2548]
  • records[2468]
  • records[1245]
  • records[5439]

Also, notice that the value of the id argument is 5439 (which happens to be one of the properties I listed above). In other words, records[id] is equivalent to records[5439] in this case.

I suggest refer these challenges / lessons on the JavaScript Objects, for some clarifications:

  • Modify an Object Nested Within an Object
  • Access Property Names with Bracket Notation
  • Check if an Object has a Property
1 Like

Great, thanks for your help. I’ll keep working on it. Appreciate all your responses.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.