Use Destructuring Assignment to Assign Variables from Objects not passing

Tell us what’s happening:

The code is returning 79 why is it not passing

Your code so far


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

// console.log(today)
function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const {today, tomorrow} = AVG_TEMPERATURES
  const tempOfTomorrow = tomorrow; // 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.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-objects

Instead of giving away the answer, please try to give hints.

Sorry, I’m new. I’ll give hints next time

ok so what are the hints? now that your post was deleted I cant see anything

It was not a hint. It was the working solution.

HINT: Think about a variable local to the getTempOfTmrw function’s scope that you can use which would be the same as using the global AVG_TEMPERATURES object.

well whats wrong with
const {today, tomorrow} = AVG_TEMPERATURES
const tempOfTomorrow = tomorrow;

Im passing all these challenges but I still dont understand deconstructing

I like to think of the following pattern when destructuring an object and need to create a variable with a different name than the original property name.

const { objectPropertyName: newVariableName } = objectName;

The above line creates two variables (today and tomorrow).

Then the line above assigns the value of tomorrow to a variable named tempOfTomorrow.

If you use destructuring properly, you will not need that second line above.

1 Like

const { tomorrow: tempOfTomorrow } = avgTemperatures;

const {today, tomorrow} = AVG_TEMPERATURES isn’t the solution.

hint: Think about it in the way if they called the function with multiple objects:“AVG_TEMP1”, “AVG_TEMP2”, “AVG_TEMP3” and they wanted you to return tempOfTomorrow from all of them. That is what the problem is trying to ask.

1 Like