Hey everyone, I’m having some issues with task 4 in the coordinate transformation exercise in the JS track. The task asks you to use memorization to store the value of the previously run function. One of the requirements is that the function stores one previous answer only. Currently, my code is storing all previous translations and I am not sure how to go about limiting the solution to storing one. My code is below:
export function memoizeTransform(f){
const cache ={
inputVal:undefined,
outVal:undefined
}
function memoize(mx,my){
const coord = [mx,my]
if(cache.inputVal == coord){
return cache.outVal
}else{
cache.inputVal=coord
cache.outVal = f(mx,my)
return cache.outVal
}
}
return memoize
}
Also, I’m trying to understand the reasoning behind the need to limit the number of results, is it for system memory management? I feel like my current solution would address that as my understanding of memoization means that as you could clear simply clear the cache (although I’m struggling to do that from within this function, so maybe I’m wrong on that )