How do a function parameters access the object

Tell us what’s happening:

Your code so far


// Setup
var collection = {
2548: {
  album: "Slippery When Wet",
  artist: "Bon Jovi",
  tracks: [
    "Let It Rock",
    "You Give Love a Bad Name"
  ]
},
2468: {
  album: "1999",
  artist: "Prince",
  tracks: [
    "1999",
    "Little Red Corvette"
  ]
},
1245: {
  artist: "Robert Palmer",
  tracks: [ ]
},
5439: {
  album: "ABBA Gold"
}
};


// Only change code below this line
function updateRecords(id, prop, value) {
if(value === "") delete collection[id][prop];
else if(prop === "tracks") {
  collection[id][prop] = collection[id][prop] || [];
  collection[id][prop].push(value);
} else {
  collection[id][prop] = value;
}

return collection;
}

updateRecords(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/80.0.3987.163 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

How do function parameters determine the object properties.
usually the comma can separate properties and determine the function parameters.
Here in this object how does it work?

the object is a global variable changed by the function. the object is not a function parameter.

the function parameters are just strings.

it is the function definition that the function should make changes to that object, based on the arguments

note: this is a training exercise, in reality it is bad practice to have a function change global variables

1 Like

Hello there.

Why do you have a screenshot in your code? Is that purposeful?

To answer your question:
I think you are getting confused by the naming convention(if a case of 1 can be considered a convention) used in the above.

You decide to refer to a function argument with any name you want. This reference is called a function parameter.
So, this line has determined the function parameters:

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

The order is important:

function updateRecords(id, prop, value) {
  // Inside the function:
  id === 5439;
  prop === 'artist';
  value === 'ABBA';
}

Now, inside the function, you are able to do this:

  1. Mutate the object by using the parameters(among other objects). This is never recommended.
  2. Have your function return a new object that is based on the original collection object, but has a few properties changed.
    Example:
//create a copy of the original object
function update(value) {
  let myObj = { ...collection };
  // Change some values
  myObj[2548].album = value;
  return myObj
}
// call the function
var updatedObj = update("New Album");

Hope this helps

1 Like

I have edited your post for readability, if you want to post a screenshot, don’t post it inside a block post, it will not show up

1 Like

My confusion is something like this:

//  this is  the first object of collection object
var collection = { 
2548: {
  album: "Slippery When Wet",
  artist: "Bon Jovi",
  tracks: [
    "Let It Rock",
    "You Give Love a Bad Name"
  ]
}
}

And in this function

function updateRecords(id, prop, value) {
  if(value === "") delete collection[id][prop];

how it determine ,

id ===2548;  
prop === 'artist or album';
value === 'track';

simply which comma in the object separate them ,like that order?

no comma in the object, it is the function call that says which values the parameters will take

in this case the function is called with id = 5439, prop = "artist" and value = "ABBA"

the connection to the object is done by using the collection variable inside the function

The order you pass arguments to a function, is the order they will be assigned to the parameters.

example(1, 2, 3);
function example(a, b, c) {
  a === 1; b === 2; c === 3;
}
//--------------------------
example(2, 3, 1);
function example(a, b, c) {
  a === 2; b === 3; c === 1;
}

If you are not understanding this line:

if (value === "") delete collection[id][prop];

Then:

  1. The if statement checks to see whether the value parameter has been passed as an empty string.
  2. If the if is true, the property prop of the property id is deleted from the collection object.

Hope this helps