const createPerson = (name, age, gender) => {
// Only change code below this line
return ({
name,
age,
gender
});
// Only change code above this line
};
The example shows :
const getMousePosition = (x, y) => ({ x, y });
but the answer for the problem is
return {
name,
age,
gender
};
My brain is farting and I’m not quite understanding why there are (parenthesis) & {curly brackets} in the example, but no (parenthesis) in the answer (I hope my question was understandable ).
It all depends upon if you want to use an implicit or explicit return. {} signals that you will not be using an implicit return, so you need to use () to fix that if you want to implicitly return an object literal.
Sooooo {curly brackets} = explicit; (parenthesis) = implicit. Since the problem example is meant to “get Mouse Position”, the code is made to specify a single expression or basically implicitly/accurately return its exact coordinates (x, y).
The answer for this problem requires an explicit return, because it’s a group of statements (name, age, gender), and this needs to be expressed explicitly with {curly brackets}.
Lol dam this exercise. Still trying to wrap my head around this, but I’m guessing, because the exercise blocked our ability to make it into an implicit expression, we had to solve it as an explicit return with {curly brackets}?
I believe I understand it on a surface level, but will still be a while until I fully comprehend everything. Thank you, like always, for helping clear some confusion for me.
I find that sometimes it’s best not to go too far down the rabbit hole when you’re trying to cram a lot of new information in. You will gradually pick up a deeper understanding as you learn anyway. Absorb what you can but keep the path ahead clear.
When using arrow functions, if you use {} after the =>, the return statement is used.
But to avoid having to write the return statement, you can simply use () which implicitly uses the return statement. This is used if you are just returning a single expression from your function.
Haha thank you. I was struggling with this at first because I was definitely overthinking it. After Jeremy helped me out in understanding better, I was able to see that the answer to this exercise REQUIRED an explicit answer, since the given code included the curly brackets and blocked off a way to answer implicitly (with parenthesis).
const createPerson = (name, age, gender) => {
// Only change code below this line
// Only change code above this line
};