Bug in exercise for ES6: Write Concise Object Literal Declarations Using Object Property Shorthand

For this exercise, if you follow the instruction of “Only change code below/above this line”, your code won’t work. You have to change the first and last curly braces to parentheses to succeed.

The execise:

Use object property shorthand with object literals to create and return an object with name , age and gender properties.

const createPerson = (name, age, gender) => {

  // Only change code below this line

  return {

    name: name,

    age: age,

    gender: gender

  };

  // Only change code above this line

};

My solution:

const createPerson = (name, age, gender) => (
  // Only change code below this line
  {name, age, gender}
  // Only change code above this line
);

you can still make it work using explicit return, instead if converting the function to implicit return

it is only the object tha needs changed, not anything else

just this part needs to be changed

2 Likes

this is also the first time in the course we encounter parenthesis wrapped around a function body with no clear explanation as to why it’s there and what it’s for.

arrow functions are introduced here: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions

it introduces both implicit and explicit return
() => {} is this a function with an empty body or returning an empty object?
and this? () => ({})

that’s why

1 Like

oooh okay I see. So if I understand you correctly the parenthesis returns the statement within the body?
thank you! I haven’t encountered the terminology of “implicit” and “explicit” yet. Just knowing the words helps clarify the concepts!

explicit return

() => {return 1}

implicit return

() => 1

the two above do the same thing

if you want to return an empty object you can’t write () => {} as that is an empty function body, situation as the first case, where the graph parentheses surround the function body
so to identify that it is not a function body but you are using implicit return to return an object you put the object inside round parenthesis

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.