Implement a higher-order function memoize(func) that returns a memoized version of the given function.
The memoized function should cache results for arguments (numbers or strings) and return cached values when called with the same input again.
This helps optimize repeated calls to expensive computations.
Example
function expensiveFunction(n) { console.log('Computing...'); return n * 2; } const memoizedExpensiveFunction = memoize(expensiveFunction); console.log(memoizedExpensiveFunction(5)); // Output: Computing... 10 console.log(memoizedExpensiveFunction(5)); // Output: 10 (cached result) console.log(memoizedExpensiveFunction(10)); // Output: Computing... 20 console.log(memoizedExpensiveFunction(10)); // Output: 20 (cached result)
JavaScript Function
No test results yet
Click "Run" to execute tests