Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript-questions#98 & error answer #220

Closed
xgqfrms opened this issue Aug 21, 2019 · 8 comments
Closed

javascript-questions#98 & error answer #220

xgqfrms opened this issue Aug 21, 2019 · 8 comments

Comments

@xgqfrms
Copy link

xgqfrms commented Aug 21, 2019

javascript-questions#98 & error answer

https://github.com/lydiahallie/javascript-questions#98-whats-the-output

image

@oshliaer
Copy link
Contributor

Yep!

The correct code is:

const getList = ([x, ...y]) => [x, y]
const getUser = user => {{ name: user.name, age: user.age }}

const list = [1, 2, 3, 4]
const user = { name: "Lydia", age: 21 }

console.log(getList(list))
console.log(getUser(user))

oshliaer added a commit to oshliaer/javascript-questions that referenced this issue Aug 21, 2019
Signed-off-by: Alexander Ivanov <oshli.a.er@gmail.com>
oshliaer added a commit to oshliaer/javascript-questions that referenced this issue Aug 21, 2019
@Pupix
Copy link

Pupix commented Aug 21, 2019

@oshliaer
image

@xgqfrms
Copy link
Author

xgqfrms commented Aug 22, 2019

the right ways

  1. normal return object
const getList = ([x, ...y]) => [x, y];
const getUser = user => {return { name: user.name, age: user.age };};

const list = [1, 2, 3, 4];
const user = { name: "Lydia", age: 21 };

console.log(getList(list));
console.log(getUser(user));
//  [1, Array(3)]
//  {name: "Lydia", age: 21}
  1. (js expressions)
const getList = ([x, ...y]) => [x, y];
const getUser = user => ({ name: user.name, age: user.age });

const list = [1, 2, 3, 4];
const user = { name: "Lydia", age: 21 };

console.log(getList(list));
console.log(getUser(user));
//  [1, Array(3)]
//  {name: "Lydia", age: 21}

image

image

refs

https://mariusschulz.com/articles/returning-object-literals-from-arrow-functions-in-javascript

@oshliaer
Copy link
Contributor

It seems it's not valid at all

=> { name: user.name, age: user.age }

But this is fine

=> { name: user.name }

@xgqfrms
Copy link
Author

xgqfrms commented Aug 22, 2019

final right question solution

const getList = ([x, ...y]) => [x, y];
const getUser = user => { name: user.name; age: user.age; }

const list = [1, 2, 3, 4];
const user = { name: "Lydia", age: 21 };

console.log(getList(list));
console.log(getUser(user));
//  [1, Array(3)]
// undefined

image

@xgqfrms
Copy link
Author

xgqfrms commented Aug 22, 2019

ref

https://mariusschulz.com/blog/returning-object-literals-from-arrow-functions-in-javascript

image

@lydiahallie
Copy link
Owner

Thank you! Could you please open a PR with the change, I can merge it asap.

@A1rPun
Copy link

A1rPun commented Sep 4, 2019

Just to be clear, this is the right solution (which is present in the provided answer):

const getUser = user => ({ name: user.name, age: user.age });

But this is not the solution to fix this answer! It clearly teaches us to use () when we want to return an object on a single line. The code in the question should be reconsidered because it now resolves in a SyntaxError: unexpected token: ':'.

@oshliaer This is not fine

=> { name: user.name }

It isn't the result you are looking for, you are defining name as a label and user.name is just a statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants