Use Destructuring Assignment to Assign Variables from Objects Test Failed

Tell us what’s happening:
I want to save the value of tomorrow in tempOfTomorrow and return it. However, “destructuring with reassignment was used” I got this when I test it. Please help!

Your code so far


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

function getTempOfTmrw(avgTemperatures) {
  "use strict";
  // change code below this line
  const {tomorrow: tempOfTomorrow} = AVG_TEMPERATURES; // 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 6.1; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

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

Within

You don’t use aveTemperatures, you need to use that in the function.

Can I call the global AVG_TEMPERATURE object in the function? I don’t get it…

No, outside of the function what you have would be correct. But inside the function it is not correct.

This is a common issue on the forums, and one we end up explaining regularly.

The AVG_TEMPERATURES variable, which is a global variable (means its accessible from anywhere, is shown here so you can see the data structure you can expect.

But, and this is key, you should avoid accessing global variables from within your function. If you write your code to access a global variable, like AVG_TEMPERATURES, then if someone else wants to use your function with their own data that follows the same format… they have to edit the global variable? Not so clean.

Instead, your function is being passed a parameter, avgTemperatures. Sure, that’s the same data as AVG_TEMPERATURES, but it’s local to your function. It’s something your function has some control over. It should know what to expect, and how to handle its own internal variables.

Is much cleaner, and its much better practice, to avoid using global variables. Your function should be written as though there were no global variables. In addition to good practice, it’s the only way you’ll pass this particular lesson.

1 Like