Im on the “Record Collection” challenge in the JS cirriculum.
Im able to pass with little to no help however I’m not totally understanding how objects relate to the functions/variables.
Let me explain my thinking:
const recordCollection has an array of objects (record ID#) that have multiple properties (album title , artist name, tracks).
The question sets you up with a function called updateRecords containg an object literal ‘records’ which contains the properties from the ID# objects in the recordCollection varable ( id(record number), prop( I assume short for property meaning things like album title, artist name, and tracks) and value (the actual name of the album/artist/songs etc.)).
-My confusion comes from the object ‘records’ and the syntax that surrounds it.
function updateRecords(records, id, prop, value) ;
is the code provided by the question.
In my head records= recordCollection.
Is this correct? If so how does JS know that records is = recordCollection?
Moreoever how does it know that id , prop, and value are equal to their correspoing information within the recordCollection object?
I hope I am able to articulate my question well enough for someone to understand my confusion .
It would be more accurate to say that records=an object with the same structure as recordCollection.
If so how does JS know that records is = recordCollection?
JS doesn’t know that.
JS knows that the variable name records will be used, within the function code, to access the first argument passed to the function when the function is called.
Moreover how does it know that id , prop, and value are equal to their corresponding information within the recordCollection object?
JS doesn’t know that either.
JS knows that the variable name id will be used, within the function code, to access the second argument passed to the function when the function is called.
Similarly for the variable names prop (third argument) and value (fourth argument).
updateRecords (somethingRandom , "not an ID number" , "not an artist", "something Random");
Im assuming I will just recieve an error code?
If im understanding what youre saying correctly:
updateRecords opperates completly independently of recordsCollection until we try and run updateRecords with recordsCollection passing in as the first argument?
Am I hitting closer to home?
Also: thanks for such a prompt concise response, I really appreciate it!
updateRecords operates completly independently of recordCollection until we try and run updateRecords with recordCollection passing in as the first argument?
Yes.
Im assuming I will just recieve an error code?
Ah, but you (the developer writing the function code) are in charge of that.
What would your code do if your function was called with those arguments?
Well, the result probably wouldn’t be very pretty.
Realistically, your code probably wouldn’t even run.
There’d almost certainly be some syntax errors, unless the first argument was a random object rather than some other data type.
The point I was trying to make though, is that JS doesn’t have any knowledge of what type of data should be in the arguments.
In real life, it’s up to the developer to handle those kinds of issues.