JS: Use Destructuring Assignment to Pass an Object as a Functions Parameters

Tell us what’s happening:

going back over some completed modules trying solutions in different ways and I came across the below which does not pass even though I’m gaining access to min and max through destructuring. i get the message indicating that destructuring was not used.

can someone point out what, if anything, is incorrect with the following solution?

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 (stats) => {
    // use function argument destructuring
    const {max, min} = 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

Please Note: I’ve already passed this challenge with

const half = (function() {
  "use strict"; // do not change this line

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

})();

but based on the example given in the challenge (below) my solution should be valid.

const profileUpdate = (profileData) => {
  const { name, age, nationality, location } = profileData;
  // do something with these variables
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:

Nothing is technically wrong, but [some of] the tests are checking the actual code you wrote, not just that the test input === the expected output. So they have to read the actual text of your solution via regex, and as a result they tend to be quite strict about what you can and can’t write, and also come up with counterintuitive error messages when you do something unexpected

1 Like

Ahh, so the test doen’t actually know whether I’m using destructuring assignment or not then? frustrating but it makes sense.

Yep: the issue is that if it just checked that it was like assert(half(stats) === 28.015), then you could just write

function half(stats) {
  return (stats.max + stats.min) / 2.0;
}

And the tests would pass. It does run that test, but for the test destructuring was used it takes the text of your answer and runs a regex: /\(\s*\{\s*\w+\s*,\s*\w+\s*\}\s*\)/g. That’s looking for (, followed by {, then the two properties, then }, then ). So when you use const {max, min} = stats, that {max, min} isn’t in parentheses, that’s actually what the regex is failing on. It doesn’t know you’ve used destructuring or not

Edit :smile: , as a demonstration, I can pass the tests with this code:

 // stats is an object!
let stats = {};
function half(stats) {
   // I just need these to be able to write `({x, y})`
  let x, y;
  // destructuring is used! kinda. And I use the comma operator
  // to only evaluate the right hand value, so `half(stats) === 28.015`
  return ({x, y}), 28.015; 
}
1 Like

I feel better. I was neck deep in the ECMAScript specification like…‘no, i’m pretty sure I’m destructuring correctly’

1 Like