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

Maybe thinking about the syntax like the following will help:

const { objectPropName: newVariableName } = objectName;
11 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

You are not supposed to use two lines. It is all accomplished in a single line. Look at the example I just gave you.

1 Like

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
11 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

That will not pass the tests. Plus, the OP has already figured it out.

1 Like

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

@RandellDawson 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

7 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?