Error Destructuring With Assignment Was Used

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

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

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

I used the above code and got it to work.
I fail to see why it is wrong to only destructure one of the values.

My question is:
The array variable is “AVG_TEMPERATURES”.
The parameter is “avgTemperatures” and so is setting the destructuring to the
variable “avgTemperatures”.

I do not understand why “avgTemperatures” is not “AVG_TEMPERATURES”.
Please help.

1 Like

Hello JSFanatic88, im not sure if I understand your doubt properly but I will try to explain.

getTempOfTmrw() is a function, in this case a parameter is passed in, and in this case is called “avgTemperatures”, it doesn’t matter if it’s called, x, a, y, param; The important is that that parameter references to the values that you pass in.

So, you are calling the function and passing the object AVG_TEMPERATURES; if so, avgTemperatures makes references to AVG_TEMPERATURES, thus those two things are the same only this time > because you are giving that value when at the end you call:

console.log(getTempOfTmrw(AVG_TEMPERATURES))

@JSFanatic88 - It may help if you can see a visualization of how values are getting passed around and used. The best tool I’ve seen for beginners in this regard is PythonTutor.

Right-click this text and open your code in a new PythonTutor tab [0].

Once there, you’ll see your code on the left and an area that visualizes aspects of your code on the right.

Just below your code are some buttons. Click the “forward >” button once to take one step into your code, then really study the visualization on the right.

Keep taking one step at a time and study the new state of the program at each step.

Hopefully this will give you some insight and help you understand the difference betweenAVG_TEMPERATURES and avgTemperatures.


[0] There’s some odd behavior when opening a link to PythonTutor code from FreeCodeCamp - if you don’t right-click, then you’ll end up on a blank page in PythonTutor, instead of a page with your code. I have no idea why :man_shrugging:

The parameter of a function is also a kind of variable. It is declared by Javascript under the hood. When you pass a value into a function, that value is assigned to the variable.

const NUM = 10;

function foo(arg) {
   console.log(arg);
}

foo(NUM);  

// Under the hood, Javascript is doing something like this (not real code)

function foo() {
   const arg = NUM;
   console.log(arg);
}

A function parameter is a local variable scoped to the function and not available outside the function. This allows the function to encapsulate a value.

1 Like