ES6: Use Destructuring Assignment to Assign Variables from Object

Hello, basically i’m blocked at this challenge (title) i written this code but isn’t working need help.

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

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

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

It can be achieved in one line and returned in the other.

You should not assign a to tomorrow, that’s not what’s being asked.

3 Likes

Mmh, how can i do it in one line, i tried this but doesn’t work ?

  // change code below this line
  const tempOfTomorrow = { tomorrow }; // change this line
  // change code above this line
1 Like

In the previous code, you were very close.
You just need to get rid of that a and replace it with something else.
Think about it.

2 Likes

My suggestion is inside of that function when you asign the new value with destructuring assignment you only need something like this:

const { tomorrow } = avgTemperatures;

and finally you can retun tomorrow const

1 Like

Mmh i tried this still not working

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

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

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

i think so that there is a extra line,

const tempOfTomorrow
2 Likes

Oooh, thank you very much guys, i finnaly get it :wink:

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
10 Likes

try this -
const {tomorrow: tempOfTomorrow} = AVG_TEMPERATURES ;

1 Like

Maybe with this example could be more clear:

const personalInformation = {
    firstName: 'Yamit',
    lastName: 'Villamil',
};

const {firstName: fn, lastName: ln} = personalInformation;

console.log(`${fn} ${ln}`);
3 Likes

Your example is very good, but it is confusing to new learners who do not know yet that in ES6: tomorrow inside an obj literal: {tomorrow} is equal to {tomorrow: tomorrow}

And, therefore, returning tomorrow will also do the work.

However, this challenge will not accept that answer, it strictly wants tempOfTomorrow returned.

2 Likes

I’m having the same issue, I think it’s something with the second test condition, I’ll have a look and come back

@camperextraordinaire found the issue

instead of using the constant, use the function param => avgTemperatures

Change from
const {tomorrow: tempOfTomorrow} = AVG_TEMPERATURES ;
to
const {tomorrow: tempOfTomorrow} = avgTemperatures;

While both solutions are correct, it’s probably hardcoded in the test

6 Likes

This is the aswer man!

const { tomorrow : tempOfTomorrow } = avgTemperatures;

i tried many times, i’ve had the same problem, but finally i could get it.

2 Likes

that was the issue. The avgTemperatures is hard-coded on the tests.

1 Like

That was an awesome explanation. Thank you!

Can you give example?

That helped, but im curious how we get access to AVG_TEMPERATURES when we dont reference it in the solution. is it just from the key tomorrow?

You call the function in the last line of the code, passing the object AVG_TEMPERATURES as argument of the function

1 Like

Just came across this problem and… wow, I definitely think the problem should be reworded.

“Use destructuring to obtain the average temperature for tomorrow from the input object AVG_TEMPERATURES , and assign value with key tomorrow to tempOfTomorrow in line.”

A much better description in my opinion would be, “In function getTempOfTmrw, use destructuring to set tempOfTomorrow to the value of avgTemperatures’ property tomorrow. Then, return tempOfTomorrow .”

I don’t know how ‘liked’ that description would be, but it’s just my 2c, since I interpreted the vanilla description in tens of ways before giving up and coming here. This is the only problem that I’ve encountered with wording that truly threw me off out of everything so far.

1 Like