I can’t get how function knows that max and min are from const stats

Tell us what’s happening:
I can’t get how function knows that max and min are from const stats.

  **Your code so far**

const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};

// Only change code below this line
const half = ({ max, min }) => (max + min) / 2.0; 
// Only change code above this line
  **Your browser information:**

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

Challenge: Use Destructuring Assignment to Pass an Object as a Function’s Parameters

Link to the challenge:

It doesn’t. Functions are not locked to only work for only one object. The destructuring takes the min and max properties from whatever object is passed into the function.

1 Like

Max certainly is not undefined inside of the function. It is defined as a function argument. That syntax means that the min and max values are extracted from what every object is passed into the function when the function is called.

2 Likes

It hasn’t been passed. So right now the program should do nothing unless the values are passed behind the scenes like a comment in previous thread suggested.

Behind the scenes half is called by passing stats .

half(stats);

Max and min are just any two variables to the function whose value are undefined only when you define it or pass something to it it gets defined inside the function.

For it to be defined inside the function, you have to create an additional line of code where you pass the object or values of max and min to the function.

1 Like

Destructuring syntax { max, min } is within parentheses the same place where the argument will be passed?

I think I was lost totally or just losing concentration.

In other words:
After I put as a parameter of half function stats object it will be destructured within parentheses by this syntax { max, min }) ?

1 Like

Yes and the argument should be an object.

Totally understandable. I too used to get stuck/confused about small things sometimes. That’s why the Freecodecamp forum community is here to help us here and there giving hints suggestions pushing small simple ideas etc.

Yes I think you are right.

1 Like

Thank you for all support. I went forward from there.

a) So, correct me if I’m wrong…but the functions sole purpose in this case…is merely to do the calculation for whatever arguments were passed to it? (in this case min and max)

b) What I also don’t understand is…because we’re dealing with objects here shouldn’t the curly brackets be incorporated within the parameter too?

Yup, whatever object is passed into the function is destructued and the average of the min and max properties is taken.

could you kindly answer (b) please? Imo…because min and max come from an object surely curly brackets should also be incorporated within the parameters too?
eg: ({…})

const half = ({ max, min }) => (max + min) / 2.0; 

I don’t understand your question. There are {}s in the function parameter list.

true… my apologies :wink:

It depends upon how you declare your object?

let myObject = {
  max: 5,
  min: 2,
}

console.log(half(myObject));

or

console.log(half({max: 5, min: 2,}));

yeah, I see the difference :wink: with curly brackets inclusive you use properties :wink: ,but in the first case you won’t be specific on any properties though, not like the 2nd case where it’s specified on 2 (in other words we want information only on 2 properties!)

The result is identical in either case.

let myObject = {
  max: 5,
  min: 2,
  foo: "bar",
  baz: "blah",
}

const half = ({ max, min }) => (max + min) / 2.0; 

console.log(half(myObject));

and

const half = ({ max, min }) => (max + min) / 2.0; 

console.log(half({max: 5, min: 2, foo: "bar", baz: "blah"}));

All this syntax means is that an object will be passed to the function and the function should extract the min and max properties of the object. Absolutely nothing is stated about any other properties the object may or may not have.


Calling and defining the function are completely separate issues. What object we pass into the function when we call the function has absolutely nothing to do with how we have defined the function.

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