Problem with ES6 Object Destructuring Lesson?

Tell us what’s happening:

I think there’s a mistake in this lesson, unless I’m missing something. How could this possibly be the solution, when the compiler doesn’t know which object the properties max and min are coming from?

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

shouldn’t the solution be this:

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

Since now you know the properties are coming from the stats object?

I tried the accepted solution in my debugger and it throws a TypeError:

Cannot destructure property max of ‘undefined’ or ‘null’.

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} = stats) => (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_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

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

Link to the challenge:

here we are creating an arrow function which is receiving an object.

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

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

console.log(half(stats)); // 28.015
// here we are calling the function by passing the object which gets destructured and only required values are passed to the function i.e. min & max.

hope this helps,
Regards,
Abhishek

Except that

console.log(half(stats))

Doesn’t exist in the solution. This is what is provided:

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

With the incorrect solution as:

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

I’m saying this is incorrect. You had to pass the stats object as an argument to evaluate it correctly.

The correct solution should be:

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

I know its not provided.
The example or challenge is little incomplete but is doing its job perfectly fine.

There is no error in the code.

The only misunderstanding you have is about the arrow function.

Just assume

as

function half ({min, max}) {
     return (min+max)/2.0;
}

both are the functions definition and both functions are doing exactly same task.

The challenge is missing one statement to call the function which is not important for that particular challenge to understand the challenge. Everything you need to understand or complete the challenge is given in the challenge itself.

The solution that you think is correct will give you some error, probably some assignment error because you wont pass the value to a function like that it.
The stats object will be passed on to function from function calling.
Example - half(stats) ;
I know which is missing from challenge but that’s not the point here.

For in-depth understanding of this, I would suggest you to review Arrow functions.