Question Regarding: ES6: Use Destructuring Assignment to Pass an Object as a Function's Parameters

HI All,

So I was able to solve this challenge, and I think I did so in the way intended (my code is below), but I am a little confused by this particular example. My confusion is about why using destructuring would be preferable in this scenario. It seems to me like you could just pass the stats object as the argument to the half function the return line (stats.max + stats.min) / 2.0 would still work fine. Is this just the exercise introducing the concept or is there some benefit to using the deconstruring assignment here?

Thanks in advance!

const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};
const half = (function() {
“use strict”; // do not change this line

// change code below this line
return function half({max, min}) {

return (stats.max + stats.min) / 2.0;

};
// change code above this line

})();
console.log(stats); // should be object
console.log(half(stats)); // should be 28.015

You are absolutely right that the code would still work. For now. But there is a more subtle concern at play here. Looking at the half function “from the outside”, or its signature if you like, you can only see that it depends on a “stats” object. What properties on the “stats” object is relevant? if the properties on the “stats” object change over time, will it break the half function? These questions can be easily answered if the signatures explicitly states what properties it expects on the argument, regardless of the size / complexity of the half function.

1 Like

Ahhh, that makes sense. Doing is with the destrucuring assignment makes what half is using more explicit and the code becomes more maintainable. Thanks for the explanation!

1 Like