JavaScript Object Deconstruction in Function Parameter

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

const stats1 = {
  max: 500,
  standard: 5,
  min: 6,
}

// Only change code below this line
const half = ({max,min}) => console.log(max,min);

// Only change code above this line

I’m having trouble understanding how HALF would chooses between the two max and mins from both variables above.

Welcome Jose.

I moved your question to its own topic because you were asking a question related to your own code for a challenge and were not answering the OP of the other thread. It is always best to create your own thread for you specific question(s). Also, it is advisable to use the Ask for Help button on the challenge, so it auto-populates with your current code and the challenge url.

Thank you.

1 Like

It doesn’t “choose”. That’s not how programming works. Just because there are two objects defined in the code there, that doesn’t mean that the program has to guess which one to use.

half is a function. The function accepts one argument, an object. To make the function work, to make it do anything, it has to be called, passing it an object.

half doesn’t do anything on its own, that’s not how functions work. What’s there is the definition of the function. You call it like half(stats) or half(stats1) – it doesn’t choose one to run against, it is given one of the objects.

1 Like

Hi Dan,

I’m mainly confused because when I run that code, It logs “56.78 -0.75” into the console. So in this case it would have to be choosing one of the variables, right? thanks for your help .

To check your code, when you press the button to submit it, the application runs a set of tests. Those tests call the function.

1 Like

ah I see ! thanks for your help!

2 Likes

There is no reference to the object (stats in this case), so how does it know to what min and max is?

in this lesson, the code is shortened and effectively removes the object reference ( the = object at the end). I hope that makes sense.

no, it doesn’t remove it
consider

let name = "Mary";
function greet(name) { 
   console.log("Hello, " + name + "!")
}

when does this function know what value to use?

in the same way, only calling half(stats) tell the function what value to use
it’s a case of confusing variable names

2 Likes

Oh! You have to pass the object into the function. Still getting used to the arrow function formatting. It’s throwing me off. Thank you. I think i get it now.

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