ES6 - Use Destructuring Assignment to Pass an Object as a Function's Parameters - How does the function know where to destructure from?

Tell us what’s happening:
Describe your issue in detail here.

Can someone please explain to me how the function knows that the keys it should be looking for to assign min and max via destructuring are within the stats object? Passing code below under ‘Your code so far’ for reference.

If you had another object that also contained min and max variables, would this confuse the function? As far as I can tell, we are not specifying anywhere within the function that it should be destructuring from the stats object.

Your code so far

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

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 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/114.0

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

Link to the challenge:

HI @msmi1433 !

Welcome to the forum!

This lies in the function call which is happening behind the scenes.

There are a few tests written for this challenge.

This first test is checking to make sure that the stats object is still on the page

assert(typeof stats === 'object');

This second test here is calling the function and making sure that the results equal the correct value

assert(half(stats) === 28.015);

When you call the function, you control the object that is passed into it.

Even if I had something like this, the result would still be the same because I am calling the half function with the argument of stats

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

const myPretendStats = {
  min: 16,
  max: 3
}

// Only change code below this line
const half = ({ max, min }) => (max + min) / 2.0;
// Only change code above this line


console.log(half(stats)) // this would return 28.015

Hope that helps!

Hi @jwilkins.oboe!

Thanks so much for your reply, makes perfect sense!

Apprecaite the help.

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