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

Tell us what’s happening:
Describe your issue in detail here.
There isn’t a real issue but a question to understand the difference between the two ways to build the code below.

I want to know if I am on the right track with my thoughts:
First snippet destructures stats, picks up properties max and min with their values and inserts them into a function as parameters. Said function needs a return statement to output the mathematic operation and apply it to half. Without return it would return undefined as default.

Second snippet destructures stats, picks up properties max and min with their values and inserts them into a function as parameters. Said function doesn’t need a return statement to apply the mathematic operation as the value of half because it has the return statement build into it.

Is this anywhere near to how it really works?
Gets kinda hard to explain but I hope I was able to be clear enough.

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

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

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

Link to the challenge:

I think you may be a bit confused by the arrow function syntax. half is the name of the function.

It might be I am. I do understand that half is the name of the function and the naming is done in a different way from: function func()

Could you point out what part of the arrow function I possibly didn’t understand or if there maybe is a more through-out explanation of the arrow function as the explanations given in excercises feel quite bare-boned.

*edit: changed in better wording

This sentence is confusing to me. There is no “apply it to half”. half is the function. half has a return statement so that it will return a value when it is called. That return statement can be explicit or implicit.

This is really for me hard to put it into words.

“Apply” as in sense that functions half return value becomes 28.015 with inserted arguments.

So “const half = ({ max, min }) => (max + min) / 2.0;” returns 28.015 as it has an automated return statement.

“const half = ({ max, min }) => { return (max + min) / 2.0;}” needs the curly brackets and a return statement inside them.
So in a sense this one doesn’t have an automated return statement.

I think you have the idea but are using confusing vocabulary. “automated return statement” isn’t the terminology you are looking for. “inserted arguments” isn’t really what you want either.


This a function definition with an implicit return value

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

This is a function definition with an explicit return statement

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

Either function definition can be used. When the function is called (or applied to an input, though that’s less common language), then you get a result.

const stats = {
  max: 3.0,
  min: -1.0,
};
const result = half(stats); // returns 1.0

const otherStats = {
  max: 5.0,
  min: -1.0,
};
const result = half(otherStats); // returns 2.0

So that is what implicit and explicit mean in this case. That seems to clarify quite a lot, atleast for now.

Also max having a value of 3.0 and min having a value of -1.0 are passed into the function as arguments is the correct wording. Not inserted.

Right…?

I’d say that stats or otherStats is the passed in as a argument when the function is called and the properties max and mix are destructured out of the argument.

Right, so we tell the function what parts of the data of now destructured argument stats we want to use.

As it actually states in the title of the exercise now that I came to think of it.

I took quite a bit of you time but this really helped a lot, thank you so much!

*edit: wording on the thanks

1 Like