Destructuring Assignment and Curly Brackets

Tell us what’s happening:
Why do I need to delete the curly brackets surrounding " (max + min) / 2.0; "? If I do delete them, my code passes the test, but the examples given show that the code within the function must have curly brackets surrounding it.

What am I missing in my understanding of this?

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}) => {
(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/80.0.3987.132 Safari/537.36.

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

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters

this is a thing about arrow function features.
Usually, with the usual function syntax, you need to use the return keyword to define the output of a function. Arrow functions let you do a thing called implicit return, where:

x => { return x + 2; }
// the above does the same of the below
x => x + 2;

but if you use the curly brackets around the body function it extect explicit return (with the return keyword)

Because those are two different things. If you have no curly braces, then JS assumes that whatever that expression evaluates to, that it should return that. If you do include curly braces, then it is a code block and JS assumes that you will be handling the return explicitly, if you want one. If you return nothing, it is undefined.

Works the way you want:

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

Also works the way you want:

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

Returns undefined because JS assumes you will give it an explicit return if you want it:

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