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

Tell us what’s happening:

Hi guys, my code works but I don’t really understand how.


const half = ({max, min}) => {

return (max + min) / 2.0;
}


I don’t see where I can specify the object I want to destruct and get its {max, min} as the parameters. What’s gonna happen, or which object will be destructed, if there were 2 or more objects above?

Thanks!

Your code so far

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}) => {

  return (max + min) / 2.0; 
}

// Only change code above this

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

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

Link to the challenge:

half is a function, so the object destructured will be the one passed to the function call, for example half(exampleObject)

Thank you ilenia!
What baffles me is that, when I passed {max, min} to the half function(as in my code), I didn’t specify {max, min } of which object I was passing (or did I)?

If there were 2 objects above, stats1 & stats2, both containing properties MAX & MIN, which object will be passed, when only {max, min} is given as parameter?

1 Like

The function definition does not care which object you use, only that the object has than two properties.

Only the function call cares which object you use.

Oh thank you so much Jeremy!
Didn’t realize I was definizeing it. My problem is solved.

Thanks again!

when you call half(stats1) it will be that object, when you call half(stats2) it will be the other object

1 Like

Just to be clear, the test will call the function for you and passes in the object stats as the argument. You can tell which function calls will happen by looking at the tests.

half(stats) should be 28.015

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