Destructuring Assignment to Assign Variables from Objects Exercise

Tell us what’s happening:
Hi I am not getting the right results. My console log statement should return 79

Your code so far


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

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const tempOfTomorrow = {tomorrow: 79} // 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36.

You are using a hardcoded value instead of making use of the avgTemperatures argument.

What about this? I use avgTemperatures argument but the exercise says I failed because i didn’t use a destructuring with reassignment

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

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

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

This is not destructuring, you are accessing the property with dot notation, which gives you the correct value but is not what is being asked from you

Ok. What would a correct example of destructuring look like in this case?

For example,

if you have an object like this,

const obj = {
  white: "#FFFFFF"
}

and you use destructuring and want to assign white to colorWhite variable you would do something like this.

const { white : colorWhite } = obj;

Then doing

console.log(colorWhite) will output #FFFFFF

For example this is destructuring;

var o = {p: 42, q: true};
var {p, q} = o;

console.log(p); // 42
console.log(q); // true

And this destructuring when you are assigning a new variable name:
Remember you can do it for one or more properties inside the object

var o = {p: 42, q: true};
var {p: foo, q: bar} = o;
 
console.log(foo); // 42 
console.log(bar); // true

Examples are from here: