ES6 - Use Destructuring Assignment to Pass an Object as a Function's Parameters

Tell us what’s happening:
My code works, but I don’t understand why as I can’t figure out what links const half to the stats object. I’m guessing that something is happening during the test, but I can’t figure out what. Is it that they are setting a variable with a line like this?

half(someObjject);

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 = (stats) => (stats.max + stats.min) / 2.0;
const half = ({max, min}) => (max + min) / 2.0;
// Only change code above this line

The stats global object already exists in the code. One of the tests just calls the half function with stats as the argument.

half(stats)

Got it, THANKS!
I get what is happening now.

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

The {max, min} acts to reach in to the “stats” that is passed into the function and extract only those 2 values and perform the operation to the right of the => sign.
The function still takes the argument “stats” just that only the max and min values will be extracted from within the “stats” object.

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