ES6: Use Destructuring Assignment to Pass an Object as a Function's Parameters In some cases, you can destructure the object in a function argument itself

I’m trying to make sense of this destructuring challenge.

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

// change code below this line
const half = ({ max, min}) => {max + min};

// change code above this line

console.log(stats); // should be object
console.log(half(stats)); // should be 28.015

I can see on the help page there is a divide by 2.0 command but this won’t work. Can you see where i’m going wrong please?

1 Like

you are missing a return statement
if you want to use implicit return you shouldn’t use the {} curly brackets
otherwise you need to use the return keyword

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Thanks. I’ve removed one set of brackets and divided by 2.0. I can’t put a return statement outside the function.

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

These are the same thing:

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

(as is this, for comparison):

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

The tests won’t pass because of operator precedence - this is a maths thing, not just a JS thing.

What is the value of this?

2 + 2 ÷ 2

And what is the value of this?

(2 + 2) ÷ 2

They are not the same

1 Like

I wasn’t putting the return statement inside the function, also I was putting divide by 2 without the parenthesis in front. So that works now, cheers.

Yeah, you can elide the brackets and the return statement if the return value is a single expression.

That might read as gibberish, so as an example:

const add = (a, b) => {
  return a + b;

Theres just a single expression there, so you can write it like:

const add = (a, b) => a + b;

If there wasn’t just a single expression, you need the brackets and the return

const add = (a, b) => {
  console.log(`a is ${a}`, `b is ${b}`);
  return a + b;

It’s a quality of life thing (makes it a bit less onerous to write stuff like => v * 2)), but it’s easy to trip up on it.

For comparison, it’s very similar to if statements or for or while loops:

// Single expression, can miss out brackets:
if (someCondition) doSomething();
// Multiple expressions, need the brackets:
if (someCondition) {

Also, just for future reference, another thing that trips people up a lot is returning objects. The syntax for objects ({}) is the same as it is for blocks, so if you write

const giveMeAnObject = () => {
  userName: "DanCouper"

It won’t work, because it’s going to think it’s a function without a return value, you need to write:

const giveMeAnObject = () => ({
  userName: "DanCouper"

Which will work fine

1 Like

I’ll take some time to digest this information. Many Thanks.

Im stuck on this right now its terrible lol

@Xennett101 how were you able to pass the last test case?

if you need help you can use the “Ask for help” button so a precompiled post will be created with also your code included (sharing your code is fundamental for getting help)

1 Like

A post was split to a new topic: Correct solutoin for Use Destructuring Assignment to Paass and Object as a Function’s Parameters not passing