Destructuring Assignment params func

how does this code refer to the object?
i was trying to make the line given in ‘hint>solution1’ refer to stats, but i’ve not cracked that. but the given solution seems to make no reference at all.

  **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/92.0.4515.131 Safari/537.36

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

Link to the challenge:

This does not refer to the object. We could have written:

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

or

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

We could use destructuring in the function body like this:

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

But we can take that last one and do that destructuring inside the parameter like this:

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

This does the same as the previous example, it just does it in the parameter list. It is passed an object and it destructures those properties out of it.

Finally, we can simplify to:

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

Does that make sense?

2 Likes

this is my issue: at wat point was an object passed to it?
i can see the object. how does the function know where to look? there are no previous examples of objects passing anonomously like this into a function (i checked but will always stand corrected).

You don’t need to assign something to a variable to use it in a function. A variable is simply a way to recall something in memory later on.

const foo = someValue => someValue;

const obj = {key: 'value'};

//this
foo(obj)

//and this
foo({key: 'value'})
//do pretty much the same thing

When you see a variable you should understand it as a value. The function is not seeking out the value, you are handing it the value directly. The variable makes things more malleable and dynamic, but it does this by being able to recall different values later on.

1 Like

so we are storing and calling from memory values of keys in an object the same as if we declare a simple variable?
ok. yeh, thanks.
just i thought we had to specify the object, like giving an address.

It is being passed by the test, invisible to us.

If you want to see it in action, you can put these at the bottom of your code:

console.log(half(stats))
// 28.015

console.log(half({ min: 100, max: 200 }))
// 150

but would it be passed as such in any other IDE?
or is the test kinda ‘hard coding’ some of the solution?

Generally test are written such that you give them a specific input and you say given this input the thing you are testing should have this given output.

You can see what FCC is directly expecting by looking in the bottom left hand corner of the challenge. This isn’t exactly what the test are, but it does inform you of there expectations.

1 Like

i think i’m getting it
the solution had not calculated a numeric answer, but gave a function that could.

by adding:

let arf = half(stats);
console.log(arf)

…(thus calling the function whilst referencing the obj) , we can get number (28…).

please confirm if i got it?

This is in the context of a unit test. The unit test tests the function. You defined the function here and it gets used elsewhere (in this case for a test).

I the “real world” you may define a function in one file and import and use it in another. You also may import it into a test file and run unit tests against it.

It’s not about IDEs. This is an artificial training environment, in the same way that a flight simulator isn’t the same as flying a plane.

or is the test kinda ‘hard coding’ some of the solution?

I don’t really understand this. The purpose of a unit test is to test if a unit (function) behaves as expected.

The test for this is:

assert(half(stats) === 28.015);

It is saying, “Hey, when I pass stats to half the result should be exactly 28.015.”

This tests to make sure the function works properly. Developers often write unit tests like this to 1) Make sure the function is working as expected. 2) Make sure the function continues to work because if someone goes in and breaks the function, the test will start failing and alert someone. This helps us sleep at night. In the real world, I probably would have written more tests for more possibilities, make sure that edge cases are tested. But this is just a silly little test for this challenge, to make sure you did your destructuring properly and the function still works.

You don’t normally write your unit tests in the same file as your code so they are always “somewhere else”. In this case (because it is a tutorial) you don’t really have access to those test, but the site uses that test to confirm your work. Yes, it is “invisible” to you, but something similar is happening every time you hit the Run the Tests button - it goes off and runs some tests behind the scenes. In this case it runs 4 tests. They are:

assert(typeof stats === 'object');
assert(half(stats) === 28.015);
assert(__helpers.removeWhiteSpace(code).match(/half=\({\w+,\w+}\)/));
assert(!code.match(/stats\.max|stats\.min/));

and they correspond to the 4 results on the bottom of the left panel.

Does that makes sense?

1 Like

The wording is a bit off, but I think the idea is right.

I would say…

The “solution” is creating a function, in this case half. The test then passes it an object (in this case stats, but it could pass it any object it wants) and sees if the function you worked on returns the value it expects.

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