ES6: Use Destructuring Assignment to Assign Variables from Nested Objects

Hello

I can’t validate this exercice on beta;freecodecamp :

This is my 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 { tomorrow : { max : maxOfTomorrow }} = LOCAL_FORECAST;
  // change code above this line
  return maxOfTomorrow;
}

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

maxOfTomorrow get the right value “84.6” but I still get the error message saying I’m not using nested destructuring :
V “maxOfTomorrow equals 84.6”
X “nested destructuring was used”

I used nested destructuring I did like in example, so where is the problem ?

Thank you

1 Like
const {tomorrow:{ max :maxOfTomorrow}}= forecast; 


16 Likes

You’re a life saver.
Btw, how did you know to do that ?

1 Like

How nehapevekar knew to use forecast:

forecast is the variable passed to the function that the assignment asks you to write. In your intitial code you used the global variable instead of the passed through parameter. In this case it worked out fine but it’s not best practice and would not have worked if LOCAL_FORECAST was defined locally within another function.

Whether or not FCC should fail this particular exercise for using LOCAL_FORECAST is another discussion…

10 Likes

I also spent quite some time around this issue as well, and even worse, I was already using forecast. My problem was that I was destructuring the whole tomorrow object so I was failing the “nested destructuring was used” criteria. This seems weird to me and the last two ES6 exercises are leaving me with the feeling that these new challenges were somehow rushed.

Here is the code that was failing me:
const {tomorrow: {min: minOfTomorrow, max: maxOfTomorrow}} = forecast;

7 Likes

I get the same error, My code is almost similar to yours. Someone please help

@dhamon I made same mistake. Its always good to learn from others :slight_smile:

1 Like

Which error are you getting? Post your solution and we can help!

This is the correct one…
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 }} = forecast; // change this line
// change code above this line
return maxOfTomorrow;
}

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

1 Like

Indeed, with a steep learning curve!

1 Like

Damm!!! I would never imagine that i’m not even a proper english speacker. thank you very much

Thank you for sharing the solution, same problem i met, and figure out the reason due to your reply!!! So Damn!!! I didn’t realize how simple it is

no its not right use forecast instead of LOCAL_FORECAST.

Thank you.
It’s working

This is how i do it:

const {today: {low:lowToday, high: highToday}} = LOCAL_FORECAST;

2 Likes