Need a clarification regarding Destructuring

Tell us what’s happening:
Could anyone explain to me how are we accessing max, min :
// const half = ({max, min}) => (max + min) / 2.0; //

without specifying that it’s located in the const stats?
What if we had more constants with min, max, average, etc. values?
Thank you in advance!

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; rv:83.0) Gecko/20100101 Firefox/83.0.

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

Link to the challenge:

the function works with any finction that has a max and min property

for the stats object in particular, just call the function with it as an argument half(stats)

But since it is outside of a function, how does it understand that we are referring to that particular min/max? What if I create 2 similar const objects and then call the given formula? I tried doing that by copying the const stats, changing the name and values of min/max and calling the code and I’m passing the challenge.

For example :

const stats = {

  max: 56.78,

  standard_deviation: 4.34,

  median: 34.54,

  mode: 23.87,

  min: -0.75,

  average: 35.85

};

const statsa = {

  max: 52.78,

  standard_deviation: 4.34,

  median: 34.54,

  mode: 23.87,

  min: -0.25,

  average: 35.85

};

const statssss = {

  max: 54,

  standard_deviation: 4.34,

  median: 34.54,

  mode: 23.87,

  min: -1,

  average: 35.85

};

// Only change code below this line

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

// Only change code above this line

nothing happens until you call the function

when you are the function with an argument you are telling the function which object to use

image


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

It makes perfect sense now. Thanks and thank you for the formatting tip!