Help with Use Destructuring Assignment to Assign Variables from Objects

Help with Use Destructuring Assignment to Assign Variables from Objects
0

#1

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


#2

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


#3

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


#4

In my reply above yours, I was trying to give a hint without telling the camper the answer.


#5

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


#6

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 @RandellDawson and @andrewma!


#7

The spread operator ... is not used anywhere in your code. Functions have parameters which are like placeholder variables which receive arguments (values) passed into the function. The order in which the arguments are passed should be the same as the parameter names representing those values inside the function.


#8

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