Bug with ES6: Use Destructuring Assignment to Assign Variables from Objects?

Hi All,

So here is the solution I came up with that will just not work!

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

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

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

Console log is telling me the answer is 79 but I’m getting the error ‘destructuring with reassignment was used’.

So then I tried this…

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

Then it worked. What gives? This version is missing one of the pieces of data from the object and this is the right answer?

You are using destructuring to capture both today and tomorrow properties’ values:

const {tomorrow: tempOfTomorrow,  today: tempOfToday} 

but the instructions and tests are expecting you to only capture tomorrow’s value and assign it to tempOfTomorrow.

const {tomorrow: tempOfTomorrow}

You’re using the object used to test the function inside the function. The function takes an argument (avgTemperatures) , and you can test it using the provided object AVG_TEMPERATURES

const {tomorrow: tempOfTomorrow} = avgTemperatures;
2 Likes

Gotcha, at the end of the day sounds like it was just weirdly worded instructions on that. They should specify they only need the one piece :slight_smile: Thanks!