1 · Model, solve and test · 25 MIN
Distinguish missing fields from falsy values
Distinguish missing fields from falsy values
A patch can omit a field or deliberately set it to an empty string, zero or false. Truthiness cannot distinguish these cases. Explicit undefined checks preserve the omitted-field behavior while accepting valid falsy replacements. The input here is trusted typed exercise data; an API must validate its patch keys and types before calling this function. Return a new object so a rejected or repeated operation cannot unexpectedly mutate the caller’s snapshot.
Absence and a falsy value are different states.
Read the example
type Learner={name:string;points:number};
function solve(input:{current:Learner;patch:Partial<Learner>}):Learner{return {name:input.patch.name===undefined?input.current.name:input.patch.name,points:input.patch.points===undefined?input.current.points:input.patch.points}}
console.log(JSON.stringify(solve({"current":{"name":"Ada","points":5},"patch":{"points":0}})));Check the expected output
{"name":"Ada","points":0}Your challenge
Input is {current:{name:string,points:number},patch:{name?:string,points?:number}}. Return the updated record, preserving omitted fields and accepting empty names and zero points.
Solution cost: O(1) for this fixed record shape. time · O(1) new record. space
Common trap
patch.points || current.points incorrectly ignores a requested zero.
Next lesson: Sort records with a deterministic tie-breaker →