Extra curly {braces} in the example

I was working through the problem trying to look at the example for reference. When I did console.log for the problem I kept getting undefined. My code below works because I used a text compare site with the solution and notice I had extra curly braces.

My code so far


const stats = {
max: 56.78,
standard_deviation: 4.34,
median: 34.54,
mode: 23.87,
min: -0.75,
average: 35.85
};

// Only change code below this line
const half = ({max, min}) =>  (max/2 + min/2); 
// Only change code above this line
console.log(half(stats))

The example provided in the challenge shows to implement the code as

const profileUpdate = ({ name, age, nationality, location }) => {
  /* do something with these fields */
}

If you notice in the third line from the bottom on “My code so far”, after => (the arrow) there are no curly braces wrapping the returning math function.

In the “example provided” the code shows the comment {wrapped in curly braces} indicating to me that the math function should be wrapped in the {curly braces}. but the following code returns “undefined”.

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

// Only change code below this line
const half = ({max, min}) =>  {(max/2 + min/2)}; 
// Only change code above this line
console.log(half(stats))

I may be reading into this deeper than I should, but I would just like some clarity on whether I am reading the example wrong, the text editors’ console is reading the code wrong or if this is just a mistake in the example code.

Very Respectfully,
Justin
2nd week :confused:

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Use Destructuring Assignment to Pass an Object as a Function’s Parameters

Link to the challenge:

Hello~!

Based on my experience with ES6 arrow functions, you don’t need the curly braces if the function is only one line. But if the function is more than one line, you need curly braces.

1 Like

I gave that a try and it does not seem to solve the problem.

My attempt at that solution

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

// Only change code below this line
const half = ({max, min}) =>  {
  (max/2 + min/2)}; 
// Only change code above this line
console.log(half(stats))

V/R Justin

If you use braces, you need a return statement.

1 Like

Thanks, If you’re in the know it be great if the problem could be updated to reflect the issue. I’m pretty new at this, so the details like that are important to me. Even your simple solution took me 3 try’s to understand how to implement it because of how new everything is.

Here’s how I used your advice to solve the problem

const stats = {
  max: 56.78,
  standard_deviation: 4.34,
  median: 34.54,
  mode: 23.87,
  min: -0.75,
  average: 35.85
};

// Only change code below this line
const half = ({max, min}) =>  {return (max/2 + min/2)};
console.log(half(stats))

Thanks for the help!
-Justin

Typically, if your arrow function is a single line return, you omit the curly braces. This is explained in the freeCodeCamp introduction to arrow functions.

1 Like

Yes, thank you. I now know it was covered briefly. But, as material is absorbed, it does not always get retained in the proper context. If this problem were to reinforce the topic it might help someone in the future. I will forever know this now because of this conversation. But, if the there is a persistence of reiterating important concepts throughout the curriculum more people may understand the core principles better.

An example if you’re interested
When I teach or tutor an advanced math course, say calculus. a student that has mastery of algebra can pick it up quite easily. But, a student that has just finished algebra for the first time recently with a B or a C may have missed some of those core concepts.

So if I teach with the assumption that they need those core reasons that most students that struggle with revisited, I only have to take seconds to mention it during the lesson. If I don’t, then I have to tutor the ones that have not yet made mastery level over minutes or even hours.

Hope this comes across as a discussion and not an argument or complaint. I like this platform and would only like to see it improve.

Very respectfully,
-Justin :vulcan_salute:

The entire curriculum is going through a dramatic re-design. In the mean time, this is exactly what the forum is here for.