Problem with "Use Destructuring Assignment to Assign Variables from Nested Objects"

Problem with "Use Destructuring Assignment to Assign Variables from Nested Objects"
0

#1

Tell us what’s happening:

FCC editor says I have the right result (84,6) but theFCC debugger says I didn’t use destructured nested assignment (but I did!)

Is my code correct??

Your code so far


const LOCAL_FORECAST = {
  today: { min: 72, max: 83 },
  tomorrow: { min: 73.3, max: 84.6 }
};

function getMaxOfTmrw(forecast) {
  "use strict";
  // change code below this line
  const {tomorrow: {max:maxOfTomorrow}} = LOCAL_FORECAST; // change this line
  // change code above this line
  return maxOfTomorrow;
}

console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:52.0) Gecko/20100101 Firefox/52.0.

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


#2

You are not quite following the instructions. See the first part of the instruction:

Use destructuring assignment to obtain max of forecast.tomorrow

You are referring to the global variable instead of the argument passed into the function.


#3

Yes but even when I code;:

const LOCAL_FORECAST = {
  today: { min: 72, max: 83 },
  tomorrow: { min: 73.3, max: 84.6 }
};

function getMaxOfTmrw(forecast) {
  "use strict";
  // change code below this line
  const {forecast.tomorrow: {max:maxOfTomorrow}} = LOCAL_FORECAST; // change this line
  // change code above this line
  return maxOfTomorrow;
}

console.log(getMaxOfTmrw(LOCAL_FORECAST)); // should be 84.6

it doesn’t work…


#4

In this code, what is the name of the only global variable?

What variable represents the argument of the function?


#5

The only global variable is LOCAL_FORECAST

What variable represents the argument of the function??-> -> -> It’s “forecast”…


#6

Exactly, so reference forecast instead of LOCAL_FORECAST as the object you are destructuring. Yes, technically both reference the same object, but FCC is trying to teach you a best practice. That best practice is, avoid referencing global variables and instead always pass in the values you need into each function and then reference the local argument variable inside the function.


#7

Ok so now it works. What I don’t understand is how the function “getMaxOfTmrw” gets array data from the global variable “LOCAL_FORECAST”??There is not link between this function and this global constant variable. At least I don’t see one…Or is it implicit?


#8

Anything declared in the global scope has access to anything else in the global scope. LOCAL_FORECAST is in the global scope. The function getMaxOfTmrw is also in the global scope, so the function has access to LOCAL_FORECAST.

The opposite is not possible. For example, if you declared a variable inside getMaxOfTmrw (let’s name it weatherIcons, you would not be able to reference weatherIcons outside of getMaxOfTmrw.


#9

Ok so its a question of global/local scope only.

Can you help me with this other exercice quickly? (the next one)? I don’t want to make a new thread of it…

After that IU’m done (for the day)

Here is my code but it doesn’t work in FCC editor:

let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
  const [b,a]= [a,b];
  // change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8


For this exercice:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays

PS: alos they should have renamed "LOCAL_FORECAST " to “GLOBAL_FORECAST” since it is a global variable :grinning:


#10

you don’t need ‘const’ here because ‘a’ and ‘b’ are already declared as variables on the very first line of the program.


#11

As @hbar1st points out, you do not want to use const here, because then b and a become new variables which are only accessible inside the anonymous function (the IIFE) and then the global variables a and b would not be affected. In this challenge, they actually wanting you to intentionally change the values of the global variables a and b.