freeCodeCamp Challenge Guide: 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 stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

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

Code Explanation

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.

30 Likes