Write a function `getUserInfo(user)` that destructures `name`, `age`, and `role` (default: `"user"`) from the object and returns a formatted string: `"name (age) - role"`.
Problem Statement
Write a function `getUserInfo(user)` that destructures `name`, `age`, and `role` (default: `"user"`) from the object and returns a formatted string: `"name (age) - role"`.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Object destructuring: const { name, age } = obj.
š” Hint 2:
You can set default values: const { role = "user" } = obj.
š” Hint 3:
Use template literals to format the string.
ā
Solution:
```javascript
function getUserInfo(user) {
const { name, age, role = "user" } = user;
return `${name} (${age}) - ${role}`;
}
```