Test 15 in Task 4 of "Coordinate Transformation" JavaScript exercise doesn't clear

I have tried to solve Task 4 of Coordination Transformation but always get failed in Test 15. It says the function “should not call the memoized function if the input is the same”. Why it expected to return value [1, 1] and why the argument is fakeTransform() not translate2d() or scale2d()?

Why it expected to return value [1, 1] and why the argument is fakeTransform() not translate2d() or scale2d()?
In Test 15, the test is checking whether you do NOT call the function again if the inputs are the same? To effectively test this, fakeTransform() is a mock function that is passed to your function which returns [1,1] when you call it first time, but returns false when you call it again. This fake function essentially verifies that you do NOT call your calculation function - since we need a special logic to check whether the function is called more than once, we can’t use the usual translate2d or scale2d which do NOT have this logic.

To make this test pass, plz store the input and the result when the function is called first time. Then in the 2nd call, check the input to see if they are the same - if yes, do NOT call the function again, but pass the stored result back. If the input is different, you can call the function again to cache the new result.

It’s work. Thanks for helping me out.

function memoizeTransform(f) {
let latestInput
let latestValue
return function (a, b){
if (latestInput === [a, b]) return latestValue

const value = f(a, b)

latestInput = [a, b]

latestValue = value

return latestValue

}
}
Please, I think I’m doing something wrong in the if statement.

Yes, I guess that the comparison is wrong in the if the statement. It would be better for you to write 2 conditions to check instead of clubbing the 2 params into a list and then checking.

When you compare primitives like Integer, boolean, the comparison works properly in most languages. But when you compare objects, the default for most language does NOT satisfy our typical expectations. Here you are intending to compare whether the 2 lists have the same data, but JS is interpreting it to see whether the 2 variables are pointing to the same memory address (reference). It is a bit of advanced territory and I am not sure about your level. Sorry if it is too much technical.

Please read this article to get a better understanding and see whether this helps you to solve the problem - if not, please post and I would try to provide a more direct answer.

How to Compare Objects in JavaScript

That’s why the If statement was useless and I got stuck for days. Thank you very much for your explanation.

1 Like