Can't pass " Use Destructuring Assignment to Assign Variables from Objects "

Can't pass " Use Destructuring Assignment to Assign Variables from Objects "
0

#1

Tell us what’s happening:
Hello everybody, so after hard reading of maybe close to an hour, I still can’t figure out really well what this lesson is trying to teach me… nor get rid of it! :stuck_out_tongue: What am I missing and what is my code missing? Thanks in advance. At.t, Pedro

Your code so far


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

function getTempOfTmrw(AVG_TEMPERATURES) {
  "use strict";
  // change code below this line
  const { today: tempofToday , 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 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


#2

You should not have changed anything above “change code below this line”. You should be using the argument that your function receives.


#3

You’re passed a parameter, and as @ArielLeslie has said, you’ve edited above this line when you shouldn’t have. The passed parameter is called avgTemperatures, while the original array is AVGTEMPERATURES – don’t redefine the parameters your function takes, simply use what you’re given.


#4

Alright, I just changed it after trying with the original, while thinking that it could be a bug, once the function is later “called?” in the line
console.log(getTempOfTmrw(**AVG_TEMPERATURES**));

edit:with the original array as the parameter, I mean.


#5

When that function is called in the console.log, they’re passing the global variable AVG_TEMPERATURES into your function, which you should access by the parameter avgTemperatures. NEVER assume there’s going to be a global that you can use, that’s dangerous practice.


#6

Thank you so much. And how shall I define the parameter inside the function body? I see the parameter isn’t the same parameter which is used to assume the returned value

(return **tempOfTomorrow**;_)


#7

How are you ASSIGNING tempOfTomorrow, given that you can’t access AVG_TEMPERATURES directly?


#8

I don’t know what you mean. My console logs returns 79
what’s been verified in the challenge first part correction.
I’m only missing the

destructuring with reassignment was used

edit: is this what you wrote the role of

use-strict

in this challenge?


#9

In the original code (like, click the reset all code button), a parameter name has already been provided. ONLY edit between the lines it tells you to, nothing outside those. For all intents and purposes, you can’t see how your function will be called. You don’t know about the existence of any other functionality, outside of your own function.

If you did that reset, you now have a parameter avgTemperatures. You don’t KNOW that this will contain AVG_TEMPERATURES, this could change at any time, perhaps being dynamically created for an API call! So, given that you only have avgTemperatures and that you’ve specified a format for that… what do you do?


#10

Right. Because when you used the original AVG_TEMPERATURES, that was the FCC error, destructuring with reassignment. By using the avgTemperatures parameter instead, you should have passed. Yes?


#11

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.


#12

And that’s exactly wrong. See where it says // change this line? You want to change that line. You’re still using the global constant, and that’s a problem.

In actual development, you can’t rely on that global constant AVG_TEMPERATURES to exist. The only thing you know that DOES exist is your own parameter, avgTemperatures. Edit your code to use your own parameter.


#13

oK, actually it is UNDEFINED until I modified it to AVG_TEMPERATURES, that is the constant where the value I shall get (79) comes from, I see no point in having this constant if it can’t be used, if it’s going to be modified dynamically, it could be using diferent sign instead of CONST.
Thanks so much for your efforts, but it didn’t make it clear. I’ll keep trying.


#14

The problem is, you are worrying about code that falls outside of your control. It truly doesn’t matter if that’s a let, a const or a baboon – YOU don’t see it. If your code were in a library somewhere, and I wanted to be able to call it, I can’t. It is tied directly to a global variable, rendering it USELESS for any other purpose.

But, by creating a parameter in the function definition, you can use that parameter instead of the global variable THAT YOU DON’T KNOW EXISTS!!! Your parameter, avgTemperatures, is the only information you actually have. Anything else is an assumption on your part, and creates brittle code. It will break unless certain GLOBAL conditions are met. That is what we in the biz call a ‘Bad Thing’. Try to avoid that.

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

In the above spoiler, I don’t reference anything in the function except that which the function actually knows about. I don’t try to force certain global conditions, I don’t assume global variables, I don’t do anything except what I know. I use the avgTemperatures object, which requires a defined format, and I return something from that.

I apologize if that seemed harsh or sarcastic, but this is something I’ve bumped my head against often, and I’ve found that having it explicitly explained why that isn’t good is the only way. I think you’re on the right track, but it’s tricky to change the way you think. It’s about thinking detail, rather than thinking big picture.


#15

Hi, i was literally working on this assignment at the same time and was having a similar issue. Your advice actually really helped drill home the point of not trying to incorporate variables outside the relevant scope.

Thanks a lot!


#16

Yes, thank you so much for your time and patience, now it makes way much more sense to me, like a parameter in a math function…


#17

I felt like i was being a little too jerky, but it something that has bitten me in the butt as a developer more than once. Scope is a BIG gotcha.


#18

When I reset the code for this lesson this is what I get…

function getLength(str) {
  "use strict";

  // change code below this line
  const length = 0; // change this
  // change code above this line

  return len; // you must assign length to len in line

}

console.log(getLength('FreeCodeCamp'));

Browser Chrome Version 68.0.3440.106 (Official Build) (64-bit)
OS X El Capitan Version 10.11.6

When I go to incognito mode and log in to FCC it seems to output the appropriate code for the lesson…


#19

well that’s random…


#20

That’s the default code for another challenge. Did your browser maybe navigate to another challenge?