November 2017
Intermediate to advanced
386 pages
9h 22m
English
The notion is also extended to internal variables, in which a local state is stored, and then used for future calls. In this case, the external state is unchanged, but there are side effects that imply future differences as to the returned values from the function. Let's imagine a roundFix() rounding function, that takes into account if it has been rounding too much upwards or downwards, so next time it will round the other way, to make the accumulated difference closer to zero:
const roundFix = (function() { let accum = 0; return n => { // reals get rounded up or down // depending on the sign of accum let nRounded = accum > 0 ? Math.ceil(n) : Math.floor(n); console.log("accum", accum.toFixed(5), " result", nRounded); accum += ...