How does the code in Record Collection really work?

I’m getting hung up on a concept of the Record Collection challenge and was looking for more clarity. It’s not about the solution but rather how this particular code works. With the Record Collection we have the initial object variable collection and then we have the function updateRecords(id, prop, value).

The part I don’t understand is how does the code know what id, prop, and value are? I understand we can have an array of arrays and we can access the nested values of one of those arrays, but when I look at the way it’s set up it just doesn’t make much sense. It seems more like it would be set up to pull the different objects/id’s for id, prop, and value variables.

Hoping someone can help me clear up my understanding of this, please let me know if I can clarify anything that isn’t too clear here.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Great Question

var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  console.log(id, prop)
  
  return collection;
}

// Alter values below to test your code
updateRecords("1245","artist", "ABBA");

I think the main problem and the answer to your solution can be found in the code above.
The updateRecords function takes 3 parameters (Id,prop,value) when we call the function

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

we are putting the string ‘1245’ in the ID field, the artist string in the "prop’ field and the “ABBA” in the value field. that is how the function knows what to interpret as what parameter.

// Only change code below this line
function updateRecords(id, prop, value) {
console.log(id, prop) //output is “1245” & output is “ABBA”)
console.log("-----")
console.log(value) " output is “undefined”)

return collection;
}

// Here is where I call the function
updateRecords(“1245”,“ABBA”);

consider the above. I only passed two strings to the function when I called it. Now updateRecords is interpreting the string 1245 as ID and “ABBA” as props. No value is is given for the value parameter.

In short the updateRecords knows what the 3 parameters when you call it.

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

1 Like

Ok I think this is clearing up something for me…

So basically, the updateRecords function works purely based on the parameters we enter? So when the function is called it would say “They put in 1245 for id field, here’s that ID…” and so on as it “looks through” the collection?

YES!!
play around with console.log.
and enter different params in the function.
that should clear things up.

the updateRecords function could be(bannana, phone, poop)

and it would still work if
if we call it (“1245”,“artist”,“abba”)

bannana would be 1245
phone would be artist
poop would be abba.

1 Like

Ok so it’s making sense now! For some reason, using logical parameters (i.e. the parameters name matches whatever data we’re looking for) was throwing me off entirely and made me assume there was some hidden part of the code that defined what an id is, what prop is, and what the value is.

I also kept seeing the line
var collectionCopy = JSON.parse(JSON.stringify(collection));
which led me to believe that there was some kind of data I didn’t understand.

1 Like

Just for reference,

var collectionCopy = JSON.parse(JSON.stringify(collection));

is just a hacky way to copy objects, that’s all - JSON.stringify just converts things to [JSON-compatible] strings, and JSON.parse converts them back to a format JS can understand. This means you can take an object - {foo: { bar: 1}} (note that it’s nested, like the record collection), run JSON.stringify, which gives you "{\"foo\": { \"bar\": 1}}", then JSON.parse spits it back out again as a new {foo: {bar: 1}}.

The reason it gets used is that, to copy a nested object, you need to manually write a function to iterate through it (a deep copy), so this is an easy way to avoid doing that.

2 Likes