Use Destructuring Assignment to Pass an Object as a Function's Parameters5567

Its official that I am spinning my wheels on this one. I must Use destructuring assignment within the argument to the function half to send only max and min inside the function.

  • stats should be an object .

  • half(stats) should be 28.015

  • Destructuring should be used.

  • Destructured parameter should be used.

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

// i tried writing the answer below a few different ways but honestly this one is //confusing me and i know my answer may not even be close.

const half = (stats) => {
  (stats.max + stats.min) / 2.0}; 
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36

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

Link to the challenge:

destructuring allows you to assign values to variables from an object

for example, you could do

const {max, min} = stats;

and the two variables will have the values of the properties of the same name from the object

in place destructuring put the assignment of a value passed in to the function to a parameter together with this

passing a value to a function is pretty much the same as assigning the value passed in to a function to the parameter using the assignment operator parameter = argument

if the parameter isn’t a single variable, but destructuring syntax, it works the same
so

function example ({var1, var2}) {}

when example is called as example(argument) it happens pretty much {val1, val2} = argument

do you think I’m any closer with this?

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

you are going closer, but do you also write parameter = argument in the function definition?

yes your are right thank you ilena! Yes it makes no sense for parameter = argument
so now i have

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

I’m getting closer, this one has been tough for me. I appreciate your help

1 Like

I am so happy, i was working on this problem for 2 days lol

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

@ilenia Thanks again !

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