Help with Use Destructuring Assignment to Assign Variables from Objects

Tell us what’s happening:
Thank you in advance for your help! What I have so will run 79 but doesn’t have the destructuring part. I’ve tried writing the code similar to the examples given with a destructuring component but always come back with an error or undefined element, even though the temperature 79 is showing up.

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 (Macintosh; Intel Mac OS X 10_10_5) 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-assign-variables-from-objects

In general it is a best practice to only access variables declared inside your function. What variable do you have access locally inside getTempOfTmrw?

1 Like

the issue is you are passing the constant AVG_TEMPERATURES as the argument of function getTempOfTmrw. But in your destructuring assignment, you are saying tempOfTomorrow = AVG_TEMPERATURES ( a constant) .tomorrow

instead, you should use const {tomorrow:tempOfTomorrow } = avgTemperatures; which will use the argument of the function instead of the constant

1 Like

yeah, what is funny is I was stuck on this exact one and I came here for help and I was like what does your answer mean and it helped me

1 Like

Ah! avgTemperatures… My confusion was definitely around the difference between avgTemperatures and AVG_TEMPERATURES and also realizing that a new variable (avgTemperatures) can be declared in the function to represent the first. It’s the spread operator that actually pulls the objects into the new function, correct?

I just tried it with avgTemperatures and it passed. Thank you so much @camperextraordinaire and @andrewma!

Oops, I meant to say the destructuring assignment :grimacing:
Thanks again, that breakdown is very helpful.