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 };
}
```