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

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

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 = (stats) => (stats.max + stats.min) / 2.0; 
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 YaBrowser/23.5.4.674 Yowser/2.5 Safari/537.36

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

Link to the challenge:

What to do here, who knows?

The line of code where (stats) is should be something like ({?+?}) instead. What they mean by turning an object into a function is something I`m not too familiar with most of the time you would have code like

Hope this helps with your question

Please make sure to provide a description of the issue you are experiencing in your own words. Being able to communicate issues is an important part of programming and helps others help you with troubleshooting.

Make sure to read the problem carefully. In this case, the problem is asking you to only send the max and min values to your function instead of the entire stats object. While your code is technically correct, it does not meet the requirements of the problem.

Use destructuring assignment within the argument to the function half to send only max and min inside the function.

destroy object what is it? I don’t know what to do with it.

Destructuring an object is when you “pull” out specific values by name instead of using the entire object. You can then reference those values as their own variables without needing to use the object.property syntax.

Let’s say you have a person object that looks like:

const person = {
  first_name: "john",
  last_name: "smith",
  age: 42,
  country: "usa"
}

Now let’s say you want to work with the first and last name values. You can pull those values out using destructuring:

function destructureExample({ first_name, last_name }) {
  return first_name + last_name;
}

destructureExample(person);

Without destucturing, this would look like:

function nonDestructuredExample(person) {
  return person.first_name + person.last_name;
}

nonDestructureExample(person);

You can read more about destructuring in JavaScript here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

1 Like

Thank you, I understood this, but the question itself in the assignment is not clear to me, it is not clear what needs to be done there?

Use destructuring assignment within the argument to the function half to send only max and min inside the function.

The problem is asking you to update the code so that the ‘half’ function is no longer directly referencing the ‘stats’ object.

// Only change code below this line
const half = (stats) => (stats.max + stats.min) / 2.0;
// Only change code above this line

You will need to update the code referenced above to remove stats, stats.max, and stats.min and replace them with their destructured equivalent.

const half = () => stats.max + stats.min 

it doesn’t work for me

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