Hi everyone! I stumbled on a (probably) missing test in ‘Coordinate Transformation’ exercise in the JavaScript track.
Here is my solution to the last task, ‘Save the results of functions’, which passed all the tests:
export function memoizeTransform(f) {
let savedX, savedY, savedResult;
return function memoize (x, y) {
if (x != savedX && y != savedY) {
savedX = x;
savedY = y;
savedResult = f(x, y);
}
return savedResult;
};
}
Later, my mentor @mayan164 explained to me that there is an error in my function’s logic:
Your last function is great, except that it should be a
||instead of a&&. When only one of the inputs changes, the result should be recomputed. Example: withfbeing the sum ofxandy, a first call withx=1, y=1has a different result than a second call withx=1, y=2. Using a&&leads to keeping the same result becausex === savedX. The tests are not always sufficiently complete to verify all cases, that’s why your solution passed all tests.
It would be helpful to have a test that checks for only one change in the inputs, to help other people who might make the same mistake. (Or maybe my mistake was so dumb that no one will ever make it again, then you can ignore this post
)