Question Bank
/

JavaScript Immutable Key Masking: Two Approaches Quiz

JavaScript Immutable Key Masking: Two Approaches Quiz

Mask the values of specified keys deeply, leaving the last three characters, two ways (recursive clone + mask and structuredClone + mask), plus two companions on path-based targeting and a JSON.stringify replacer variant.

Question Bank
Hard
JavaScript
quiz
references
immutability
interview-prep

385 views

11

Implement maskObject(obj, keys) using a RECURSIVE deep clone that masks any value whose key matches keys, leaving the last three characters and replacing the rest with #. Do NOT mutate the input.

Examples

Example 1:

Input:
obj = { name: "foo", age: 21, sin: "1234-829", account: { name: "saving", number: "92810-00928" } }
keys = ["sin", "number"]
Output: { name: "foo", age: 21, sin: "#####829", account: { name: "saving", number: "########928" } }
Explanation: Each match keeps the trailing 3 chars; everything else is replaced with "#" of the same length.

3 more questions, with full solutions and explanations, are available for premium members.

Upgrade to Premium