Explorer
Node.js

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

Config Manager with Defaults

Medium
Create a `createConfig(defaults)` that returns a config manager with `get(key)`, `set(key, val)`, and `getAll()` methods. Defaults are overridable.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"port":8080,"host":"localhost"}