ES6: Use Destructuring Assignment to Assign Variables from Object

ES6: Use Destructuring Assignment to Assign Variables from Object
0

#1

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

#2

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

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

#4

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.


#6

Maybe thinking about the syntax like the following will help:

const { objectPropName: newVariableName } = objectName;

#7

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


#8

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

#9

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


#10

i think so that there is a extra line,

const tempOfTomorrow

#11

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

#12

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


#13

Maybe with this example could be more clear:

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

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

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

#14

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


#15

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.


#16

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


#17

@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