Explorer
Node.js

Write a function `setIn(obj, path, value)` that returns a new object with the value set at the given dot-notation path, without mutating the original.

Problem Statement

Write a function `setIn(obj, path, value)` that returns a new object with the value set at the given dot-notation path, without mutating the original.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Create shallow copies at each level of the path. šŸ’” Hint 2: Split the path by "." and walk through, copying each level. šŸ’” Hint 3: Use reduceRight or iterate through path segments creating new objects at each step. āœ… Solution: ```javascript function setIn(obj, path, value) { const keys = path.split("."); const result = { ...obj }; let current = result; for (let i = 0; i < keys.length - 1; i++) { current[keys[i]] = { ...current[keys[i]] }; current = current[keys[i]]; } current[keys[keys.length - 1]] = value; return result; } ```

Immutable Update Utility

Hard
Write a function `setIn(obj, path, value)` that returns a new object with the value set at the given dot-notation path, without mutating the original.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"origC":1,"updatedC":99,"sameD":true}