Create a `createConfig(defaults)` that returns a config manager with `get(key)`, `set(key, val)`, and `getAll()` methods. Defaults are overridable.
Problem Statement
Create a `createConfig(defaults)` that returns a config manager with `get(key)`, `set(key, val)`, and `getAll()` methods. Defaults are overridable.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Store the config as a shallow clone of defaults.
š” Hint 2:
get() returns the value for a key. set() overrides it.
š” Hint 3:
Use spread to merge defaults: const config = { ...defaults };
ā
Solution:
```javascript
function createConfig(defaults) {
const config = { ...defaults };
return {
get(key) { return config[key]; },
set(key, val) { config[key] = val; },
getAll() { return { ...config }; }
};
}
```