Use Destructuring Assignment to Pass an Object as a Function's Parameters_2

Use Destructuring Assignment to Pass an Object as a Function's Parameters_2
0

#1

Tell us what’s happening:
I think the challenge answer may be wrong. It claims the answer should be 28.015 with values of max: 56.78 and min: -0.75. stat.max + stat. min = 56.03 not 28.015. There may be something wrong with my code, and if there is please let me know, but I ran this in Visual Studio Code and got the correct answers.

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
};
const half = (function() {
  "use strict"; // do not change this line

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

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

FREECODECAMP ERROR

// running tests
half(stats) should be 28.015 
Destructuring was used.
// tests completed

VISUAL STUDIO CODE RESULT

{ max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85 }
56.03

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters


#2

Hi @ACF !

You do have some problems with your code. One of them is that you’re not using the variables you created from destructuring:

return (stats.max + stats.min)

I would suggest resetting the code and following the second example in the instructions:

const profileUpdate = ({ name, age, nationality, location }) => {
  /* do something with these fields */
}

In this case, it’ll look more like:

return function profileUpdate({ name, age, nationality, location }) = {
  /* do something with these fields */
}

#3

As @camper pointed out you are using stats.max and stats.min which is not an example of object destruction.

Also destructing an object inside the function body is not beneficial as it allows the function to take the full object as a parameter.

Here is the correct code

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({max, min}) {
    // 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


#4

Thank you. Your solution worked. I accidentally deleted the 2.0 in the denominator. That’s why I was getting the answer I was getting.