Files
Lauren Van Sloun 4507a6b254 Minor grammar fixes (#36643)
* Minor grammar fix
2019-08-20 14:36:22 +01:00

1.5 KiB

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

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


Problem Explanation

You could pass the entire object and then pick the specific attributes you want by using the . operator, but ES6 offers a more elegant option!


Hints

Hint 1

Get rid of the stats and see if you can destructure it. We need the max and min of stats.


Solutions

Solution 1 (Click to Show/Hide)
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
})();

Notice that we are destructuring stats to pass two of its attributes - max and min - to the function. Don't forget to the modify the second return statement. Change stats.max to just max, and change stats.min to just min.

Solution 2 (Click to Show/Hide)

Here is another solution that works. Not much of a difference, other than the fact that the function doesn't have a name.

const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
  return ({ max, min }) => {
    // use function argument destructuring
    return (max + min) / 2.0;
  };
  // change code above this line
})();