Record Collection: Object Literal and function parameters

Tell us what’s happening:
Describe your issue in detail here.

How does the code know that records is the object literal and the id is one of the property keys inside of it?

How are the function parameters linked to the object itself?

  **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) {
  if (prop !== 'tracks' && value !== "") {
    records[id][prop] = value;
  } else if (prop === "tracks" && records[id].hasOwnProperty("tracks") === false) {
    records[id][prop] = [value];
  } else if (prop === "tracks" && value !== "") {
    records[id][prop].push(value);
  } else if (value === "") {
    delete records[id][prop];
  }
  return records;
}
updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Record Collection

Link to the challenge:

1 Like

The values for the function parameters are provided at the time the function is called. When you click the “Run the Tests” button, the system calls your function several times, passing in the appropriate values for each parameter. Notice the last line of the code in which the function is being called:

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

recordCollection is the object defined at the top of the code and the other three parameters are values. The function itself does a bunch of checks on those values and then takes the appropriate action based on what was passed into it. The tests are calling your function like this several times with different values to make sure your function works correctly.

Does that answer your question? Or were you referring to something else?

I do understand the parameters of the function itself but this:

records[id][prop] = value;

is what I don’t fully understand.

Does this point to the object[property][property_key] and assing its value to key_value?

And why are they array enclosing? [ ]

Maybe you need to review the last few lessons about working with objects, particularly accessing object properties with variables

What can I research to fully understand that?
I’m not being capable of expressing what my doubts are here :sweat_smile:

this syntax here is accessing an object property. With the = in front, it is assigning a value to that property.

Objects in JavaScript is the general topic

Try to ask your questions here, that is also an important skill to learn

1 Like

I will research a bit more about objects because I’m not sure why I don’t understand the function when it is using its own parameters to refer to the object properties, keys and values… Thank you so much.

Ilenia thank you so much again for helping me.

After hours of research and almost giving up about learning to code I just realized that I need to explain every single line of code as if I was 5.

I think the most stressful thing for me about this kind of exercises is not being able to code them by myself and having to look after the solution. And also the amount of new concepts that are dropped above you.

I’ve realized how to understand what I wasn’t understanding before.

I will answer myself with some of the steps I’ve done to understand my doubts.

Starting with updateRecords( ) as a function that takes as parameters (records, id, prop, value)
where arguments are:

  • records = Object Literal
  • id = nested object (number)
  • prop = nested object property name (i.e. “artist”)
  • value = value of the property of the nested object (i.e. “Robert Palmer”)

This can be seen when the function is declared at first:

function updateRecords(records, id, prop, value) {
//function code here
};

And the arguments are given at the end of the code. (And fCC tests other arguments out of the code itself)

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

So, when the function needs to access the albums of recordsCollection it will always be records[id] because this points to the nested objects which are named by numbers. Otherwise will search for the parameters in the object literal and wouldn’t be able to find them (because they’re not), will only find numbers.

This is what I needed to do for understand all the lines in the code:

function updateRecords(records, id, prop, value) {
if (prop !== "tracks" && value !== "") { 
// If prop argument is strictly different from "tracks" AND value is strictly NOT an empty string

	records[id][prop] = value;
	// invoke Object literal(recordsCollection) with the specific nested object (id) and the property of it; assigns value to the value of that property

}else if (prop === "tracks" && !records[id].hasOwnProperty(prop) ) { 
//If the given argument of prop is strictly "tracks" and recordCollection with it's nested object returns false when checking for tracks
       // !records.hasOwnProperty(prop) IS ALSO records.hasOwnProperty(prop) === false
       // When searching with .hasOwnProperty() this can be either ("tracks") or (prop) because you're earlier conditioning that prop has to be strictly "tracks"

	records[id][prop] = [value];
	//invoke Object literal(recordsCollection) with the specific nested object (id) and the property of it; then assign an array with a value using the value parameter

}else if (prop === "tracks" && value !== ""){
//If the given argument of prop is strictly "tracks" and the value has something (is not empty)

	records[id][prop].push(value);
	//invoke Object literal(recordsCollection) with the specific nested object (id) and the property of it and uses .push() method to add the value at the end


}else if (value === ""){
// If the value of value argument is strictly empty

	delete records[id][prop];
	// deletes in the Object Literal (recordsCollection) with the specific nested object the property

}

  return records;
}

If I’m being wrong or something does not make sense, please let me know. I just tried to figure out every single thing that the code does to understand it better, and try to explain with my best words.

Now the best thing you can do for yourself is wait a couple of days, then do the challenge again without looking at the solution
If you keep relying on the solutions you will not get better at solving algorithms

1 Like

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