-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge-13.js
37 lines (31 loc) · 1.45 KB
/
Challenge-13.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*Challenge 13
Problem Statement: Implement a memoization function.
Description: Write a function that takes a function as its argument and returns a memoized version of the function.
Solution Approach: Store the function's results for previous inputs and return the stored result for repeated inputs.*/
function memoize(func) {
const cache = new Map();
return function (...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
else {
const result = func(...args);
cache.set(key, result);
return result;
}
};
}
// Example functions to be memoized
function multiply(x, y) {
console.log('Multiply Numbers..');
return x * y;
}
// Memoized versions of the functions
const memoizedMultiply = memoize(multiply);
// Example usage:
// Using memoizedMultiply
console.log('Performing expensive calculation = ', memoizedMultiply(4, 5)); // Calculated result, Performing expensive calculation, Result: 20
console.log('Memoized result = ',memoizedMultiply(4, 5)); // Memoized result , Performing expensive calculation ,Result: 20
console.log('Performing expensive calculation = ',memoizedMultiply(5, 5)); // Calculated result, Performing expensive calculation, Result: 25
console.log('Performing expensive calculation = ',memoizedMultiply(6, 5)); // Calculated result, Performing expensive calculation, Result: 30