Destructured paramaters: ES6

Tell us what’s happening:
I clearly don’t get the destructured parameters.
I tried putting min and max as parameters ({min,max}) but it said min was not defined.
Then I tried changing only the function parameters, leaving the whole ‘stats’ when calling the half function. Then it at least gave a result.

BUT, my answer is 28.765 not 28.015, because min is -0.75.
56.78 - (-0.75) = 57.53
Divide that by 2 and it is 28.765. (i.e. (56.78+0.75) / 2)
But below the RUN TESTS, ETC, it says:-
half(stats) should be 28.015 (which is (56.78-0.75) / 2)

When I RUN TESTS again, it still says:-

half(stats) should be 28.015
Destructuring should be used.

What gives?
Your code so far

// AT FIRST I HAD:-
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
function half(stat) {
const {min:hmin,max:hmax} = stat;
const hcut = (hmax - hmin) / 2.0;
return hcut;
};

const halfCut = half (stats);
console.log("hcut",halfCut);
console.log("half",half(stats));
// Only change code above this line

// THEN I TRIED:-
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
function half({min,max}) {
  return (max - min) / 2.0;
};

const halfCut = half (stats);
console.log("hcut",halfCut);
console.log("half",half(stats));
// Only change code above this line

IT GIVES THE SAME RESULT IN console.log (28.765)

  **Your browser information:**

I am using Edge in Win10.

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 Edg/93.0.961.52

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

Link to the challenge:

With mathematics subtracting a negative number is actually the same as adding the positive number.

56.78 - (-0.75) = 57.53
56.78 + 0.75 = 57.53

Here you can read more about it Adding and Subtracting Positive and Negative Numbers (mathsisfun.com)

I’ve even tried
‘’’‘js
function half(stat) {
const {min:mn, max:mx} = stat;
return (mx + mn) / 2.0;
‘’’’
Test: Destructuring should be used.
I changed “max - min” to “max + min” to match the 28.015 expected. . . . .

My problem is not understanding Maths. But thanks.
I cannot get passed the *** Destructuring should be used. ***
I really need help with that. Please.

Got it . . .
const half = ({min,max}) => (max - min) / 2.0;
That is what they were looking for. . . . sorry to bug you guys. Silly me.
Btw. I changed -0.75 to 0.75 for the purpose of the test.

for the test it doesn’t change anything as the test calls the function with its own argument, like half({min: -0.75, max: 56.78})

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