freeCodeCamp has a lesson titled “Use Destructuring Assignment to Pass an Object as a Function’s Parameters”. In that lesson they provide the following code:
const profileUpdate = (profileData) => {
Const {name, age, nationality, location} = profileData;
// will refer to this code block as A
They then go on to state: “This effectively destructures the object sent into the function”.
From what I understand, in A) profileUpdate is the function identifier and profileData is the function parameter. In this case, any argument that the function profileUpdate would accept would ostensibly have to be an object with the property identifiers of name, age, nationality and location given the second line of the code up above. After receiving just an object, profileUpdate would respectively create variables of name, age, nationality and location and assign them the value of respectively, profileData.name, profileData.age, profileData.nationality and profileData.location. Again, I believe this to be correct but if I have misunderstood anything please feel free to correct me. // will refer to this paragraph as C.
Now, freeCodeCamp splits their webpage in half. On the left side of the page is the lesson and on the right side of the page is a problem to test your comprehension of the lesson. On the left side of the page they gave us the following object:
const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 35.54,
mode: 23.87,
min: -0.75,
average: 35.85
};
// will refer to this object as B
So i decided to take the general structure of A and apply it to B to verify that I truly understood A. I wrote the following in VScode:
const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};
const statsUpdate = (x) => {
const {max} = x;
}
statsUpdate(stats);
console.log(max);
console.log(typeof max);
expecting the values of 56.78 and object to appear but instead I get a reference error stating “max is not defined”. I’m clearly not understanding something in paragraph C up above or I am not writing the right expressions in the code immediately up above. What am I doing wrong? I have tried to see the error but I’m not capable of seeing it right now and your help would be greatly appreciated.