Something wrong with ES6: Use Destructuring Assignment to Pass an Object as a Function's Parameters

Hi,

This code does not work fully. How could I improve it? This part works:" stats should be an object ."

But these don’t:" half(stats) should be 28.015" and
“Destructuring was used.”

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
  return function half(stats) {
    // use function argument destructuring
    stats = ({ max, standard_deviation, median, mode, min, average }) => {
    return (stats.max + stats.min) / 2.0;}
  };
  // change code above this line

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

I tried this for example but nothing seems to work.

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
    function half({min = -0.75 ,max = 56.78}) {
    // use function argument destructuring
    return (max + min) / 2.0;
  };
  // change code above this line

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

Now I tried this and everything else is ok, except this: “destructuring was used”.

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
  return function half(stats) {
    // use function argument destructuring
    const{min,max} = stats;
    return (stats.max + stats.min) / 2.0;
  };
  // change code above this line

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

this is destructuring, ok
so now you have variables min and max - so why are you still using stats.min and stats.max?

the other thing is that destructuring should be happening in place
when you do it in place, like in function parameters, you just need the stuff inside the graph parenthesis

2 Likes

In function parameters? I tried but it got really messy.

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};
const half = (function() {
  "use strict"; // do not change this line

  // change code below this line
  return function half(stats) {
    // use function argument destructuring
    const{min,max} = stats;
    return (max + min) / 2.0;
  };
  // change code above this line

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

the nearest you have been is this, but this is still wrong

destructuring in place is not messy if done correctly

1 Like

Think of using the value passed to the parameter stats, like you are here.

const {min , max} = stats;

Only when using the parameter you do not need const or = stats because the value (the object) is being passed to the parameter.

const user = {
  name: 'Joe',
  age: 27
}

function getUser({name, age}) {
  return name + ' is ' + age + ' years old'
}

getUser(user)
// returns "Joe is 27 years old"
2 Likes

Thanks a lot :slight_smile: I got it right now :sweat_smile: