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

I dont understand this solution. The task: Use destructuring assignment within the argument to the function half to send only max and min inside the function.

The solution is listed below. But how can this be the solution when the solution doesnt even reference the “stats” object? What if theres another object that also contains both “max” and “min” properties?

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 }) => (max + min) / 2.0;

// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

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

Link to the challenge:

1 Like

No, the solution was to create the function, not use it.

Somewhere there is a test running in the background calls half(stats) and checks to see if that returns the correct value. Since the function does not reference a specific object, but simply destructures whatever object it is passed, it can also be used/tested with other objects.

Are you saying that if there are other objects that also contain max and min properties, then the function listed above would return the values for all those objects?

Sorry, I get confused easily. But thanks in advance Silent Bob!

Let’s say I have this function:

const range = ({ min, max }) => max - min

It doesn’t care what I am going to call it with or what its name is and it doesn’t care. All it cares is that it will get a reference (memory address) to an object and it will grab the min and max properties from it and use those in the function.

I could call it like this:

const obj = {
  min: 12,
  max: 192,
}

console.log(range(obj))

Or I could call it like this:

const elephantPajamas = {
  foo: 'hey',
  max: 192,
  bar: false,
  min: 12,
  baz: [1, 3, 5],
}

console.log(range(elephantPajamas))

It doesn’t care what the name of the function is or what it is called.

It doesn’t even care if it has a name:

console.log(range({ min: 12, max: 192 }))

It doesn’t know or care. All it knows is that it will be passed an object and it will grab the min and max properties from that. That is the full extent of what it knows (or cares) about the object it is being passed.

In the challenge, the test, invisible to you, is testing your half function by calling it with different objects and seeing what it returns.

3 Likes

It took me longer than it should but, thanks to you I now understand my confusion. The exercise was just asking me to create a function, not to actually use or call it. I have a paralyzing tendency to over complicate things. A tendency to assume everything is calculus when most of the time its just basic algebra. Doh

Thanks again!

Go easy on yourself - this is tough stuff. You’re not the first person to ask this question - I know I’ve answered it several times.

1 Like

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