Use Destructuring Assignment to Assign Variables from Objects Need Help Plz

I’m fairly certain that I am on the correct path with the code I am using I just can not figure out why it is returning this error. I get 79 to appear in the console but it also says this:

// running tests
destructuring with reassignment was used
// tests completed

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; rv:62.0) Gecko/20100101 Firefox/62.0.

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

Your code returns 79 for tomorrow’s weather every time I run that function regardless of what argument I give it, which is clearly incorrect.

For example, the temperature tomorrow here is clearly 68. But your code tells me it’s 79, which is obviously wrong:

getTempOfTmrw({ today: 65, tomorrow: 68 });

I’ll go back and try to figure it out.

I’ve spent way too much time on this lesson can someone just explain to me what I should be doing? I researched as much as I could, watched YouTube videos explaining this subject and I still cant figure out whats going on.

The function takes an argument, you aren’t using the argument. This isn’t specific to ES6, it’s just like every other function you’ve written. For a simpler example:

const TWO = 2

function add(a, b) {
  return TWO + TWO;
}

add is a function that takes two numbers and adds them together. But no matter what numbers you give it, it returns 4. The function is broken. Why is it broken?

1 Like

It broken because when you call the function you are not passing any variables and simply telling it to add TWO + TWO. I think…

Still doesn’t make any sense to me. The very next lesson I get and I solved with no problem. Everything the lesson lays out in its example is what I am using to solve and its coming back that I am missing something

Which is very much what you are doing in your function for this challenge

Hey @JoseBara,
Let me try to explain this to you.

You are creating your function in manner that it will work only on AVG_TEMPERATURES object.
But, what if there was another object like :

const AVG_TEMPERATURES1 = {
  today: 75.5,
  tomorrow: 80
};

?
You will have to write function for that too.
So, you should use the argument passed in your function instead of actual object name and your function will be applicable for every object.
Does this help?

Sort of. If I do:

add(5, 3)

I still get 4. The ‘variables’ a and b are being set. It’s just they’re not being used.

We’d fix the code by doing this:

function add(a, b) {
  return a + b
}

In a similar way, let’s take a look at your function:

function getTempOfTmrw(avgTemperatures) {

If we don’t involve avgTemperatures somehow, we’ll be making the same mistake as the add function.

  const {tomorrow : tempOfTomorrow} = AVG_TEMPERATURES; // change this line

Notice that we haven’t used avgTemperatures. Instead, we’ve used a different variable, called AVG_TEMPERATURES. Notice that AVG_TEMPERATURES never changes.

Thank you, I kept looking at avgTemperatures and for some reason thought it was the same as AVG_TEMPERATURES…