Explorer
Node.js

Write a function `findUser(users, name)` that returns the user object and its index as `{ user, index }`.

Problem Statement

Write a function `findUser(users, name)` that returns the user object and its index as `{ user, index }`.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Array.find() returns the first element that matches a condition. šŸ’” Hint 2: Array.findIndex() returns the index of the first match. šŸ’” Hint 3: Use both methods together and return an object with user and index. āœ… Solution: ```javascript function findUser(users, name) { const user = users.find(u => u.name === name); const index = users.findIndex(u => u.name === name); return { user, index }; } ```

Array.find and Array.findIndex

Easy
Write a function `findUser(users, name)` that returns the user object and its index as `{ user, index }`.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"user":{"name":"Bob","age":25},"index":1}