Use Destructuring Assignment to Pass an Object as a Function Parameters

Tell us what’s happening:

So I thought the solution was just

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

But the solution said


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}) {
    // use function argument destructuring
  return (max + min) / 2.0;
  };
  // change code above this line

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

Can someone explain why we had to return an identical function inside a function in order to solve this problem?

Link to the challenge:

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters

half() returns a function rather than a value

1 Like

i have the same question as yours, and i try the two methods:

first, i try :slight_smile:

> const half =(function(){
>   return ({max, min}) => (max+min)/2.0;
> })();
> 
> console.log(half);

the 1st output:

function (_ref) {
    var max = _ref.max,
        min = _ref.min;
    return (max + min) / 2.0;
  }

then, i try :slight_smile:

> const half2 = ({max,min})=>(max+min)/2.0;
>
> console.log(half2);

the 2nd output:

function half2(_ref2) {
  var max = _ref2.max,
      min = _ref2.min;
  return (max + min) / 2.0;
}

So, the 2nd is a function named ‘half2’ while the 1st one is no-name.

It is not clear if you are only changing the part between the comments. Can you post the full code for each one you tried. The part you have below looks promising, but if that is the only code you have in the editor, it will not work.

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