Use Destructuring Assignment to Assign Variables from Objectss

Tell us what’s happening:
Why wont this pass i am using destructuring here properly i believe.

Your code so far


const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const { tomorrow : tempOfTomorrow } = AVG_TEMPERATURES; // change this line
  // change code above this line
  return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects/

You are destructuring, but you are doing it from the global constant, not the variable being passed to the function. In this case, they are the same thing but in other cases they may not.

The instructions aren’t very clear, but we can infer what they want because it is best practice and otherwise the passed in variable would never get used.

3 Likes

Oh thank heavens! I was stuck on this too. I used the global variable and didn’t even notice they passed it into the function. I’ve used this hundreds of times at work and I was thinking to myself ‘what the heck is going on here?!’

Hello,

I can not get past this test “destructuring with reassignment was used”.

Here is my code:

const AVG_TEMPERATURES = {
  today: 77.5,
  tomorrow: 79
};

function getTempOfTmrw() {
  "use strict";
  // change code below this line
  const { tomorrow : tempOfTomorrow } = AVG_TEMPERATURES; // change this line
  // change code above this line
  return tempOfTomorrow;
}

console.log(getTempOfTmrw(AVG_TEMPERATURES)); // should be 79

Any insights?
Thank you.

You shouldn’t remove the function parameter from the function declaration, and you should destructure the function parameter, not the global object

1 Like

Thanks! :slight_smile: