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

How does
const half = ({max, min}) => (max + min) / 2.0;
know it’s being extracted from stats?

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/115.0.0.0 Safari/537.36

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

Link to the challenge:

Because the function is being called as:

half(stats);

You could create another object that has a max and min property (let’s call it moreStats) and pass it into the function:

half(moreStats);

And it works the same way.

Probably it would less confusing if the starter code didn’t use the same name for both the object and the parameter name in the function. The stats parameter in the function does not mean that it has to use the stats object defined above.

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