0

I have this Homework that requires me to pass a parameter called userID to an arrow function. Inside this function, I am required to use the .find function on an array called users. this find function should return an object containing the userID. Here is the full instruction :

Create a getSelectedUser arrow function above displaySelectedUser. It should take a userId parameter and use the Array .find function on the users collection to find and return the selected user object. Your .find call should take an inline arrow function and de-structure its parameter to obtain the id property.

the users is an array: users =

[{
  age: 50,
  weight: 55,
  height: 6,
  country: 'US',
  name: 'Bob Manuel',
  id: 'ehriuiuye'
},
{
  age: 20,
  weight: 80,
  height: 6,
  country: 'UK',
  name: 'Michael Lawrence',
  id: 'fjikijd'
}];

what I have done

const getSelectedUser  = (userId) =>{
    users.find(element => element.id === userId);
};

Now, the auto-grader returns this error:

Create a "getSelectedUser" function that returns the selected "user object". See the instructions for details.

Is there anything wrong with the function I created?

9
  • The answer will depend on what language you are using, which you neglected to identify. Commented May 6, 2019 at 19:41
  • From the assignment and sample code I would guess this to be JavaScript (arrow functions, destructuring, triple equals sign for comparisons ===) Commented May 6, 2019 at 19:47
  • 1) you didn't return anything 2) not sure a semi colon goes at the end of a JS function, Hope this helps - WWC Commented May 6, 2019 at 20:42
  • I’m sorry it’s JavaScript Commented May 6, 2019 at 20:55
  • @Wookies-Will-Code you're right of course, nothing is returned. The semicolon is fine though ;) Commented May 6, 2019 at 21:23

2 Answers 2

1

try this:

const getSelectedUser = (userId) => users.find(({id}) => id == userId)
Sign up to request clarification or add additional context in comments.

Comments

0

I guess what could be missing is the "destructuring" mentioned in the instructions. Destructuring is the process of assigning selected properties of an object to variables. For example, if you have an object

const foo = { 
    bar: "hi",
    baz: "friend"
}

you could either access it’s properties via foo.bar and foo.baz or by destructuring the object with

const {bar, baz} = foo;

which would define two variables bar and baz.

In your case you could do the destructuring instead of specifying the element parameter of the arrow function.

Edit: As was pointed out in a comment, you're not returning anything from the getSelectedUser function. You would need to do return user.find(element => ...) or maybe return user.find(({id}) => ...)

Edit 2: The full code should be:

const getSelectedUser = (userId) => {
    return users.find(({id}) => id === userId);
};

or the short version:

const getSelectedUser = (userId) => users.find(({id}) => id === userId);

3 Comments

If I do the destructuring, I will get the userID in a variable. What do I do with it?
You could replace the element variable and subsequent element.id property access in your inline arrow function with a destructuring expression ({id}) => ...
Thanks. I’m still confused . How do I return the object after destruction (to get the ID). I’d appreciate if you could help write the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.