Cache With Time Limit
A key/value cache where every entry expires on its own timer — and setting a key again restarts its clock.
JavaScript · ES6 — Pratik Rai ·
A class that decides what it means to add two of its instances, or print one.
Build an ArrayWrapper class taking an array of numbers.
Adding two instances with + gives the sum of every number in both. Passing one to String() gives the numbers comma-separated inside square brackets.
Input:
JSfile.javascript1const a = new ArrayWrapper([1, 2]); 2const b = new ArrayWrapper([3, 4]); 3a + b; // 10 4String(a);
Output:
"[1,2]"
valueOf answers the numeric context, toString the string one.
Goal: Implement valueOf and toString so both coercion contexts behave.
Continue learning with these related challenges
A key/value cache where every entry expires on its own timer — and setting a key again restarts its clock.
JavaScript · ES6 — Pratik Rai ·
Fold an array of functions into one, applied right to left.
JavaScript · ES6 — Pratik Rai ·
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6 — Pratik Rai ·
A key/value cache where every entry expires on its own timer — and setting a key again restarts its clock.
JavaScript · ES6
Pratik Rai ·
Fold an array of functions into one, applied right to left.
JavaScript · ES6
Pratik Rai ·
A once wrapper: the first call runs, every call after it does nothing.
JavaScript · ES6
Pratik Rai ·