From ecd31a45842827a3a1c14c5d812c8a1e48344398 Mon Sep 17 00:00:00 2001 From: puneet222 Date: Sat, 3 Oct 2020 00:14:15 +0530 Subject: [PATCH] Added question related to closure from my interview experience --- topics/closures.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/topics/closures.md b/topics/closures.md index b311f25..3c0dc9f 100644 --- a/topics/closures.md +++ b/topics/closures.md @@ -108,3 +108,17 @@ console.log(hof(3, 4)(5, 6)(7)); // 25 console.log(hof(4, 5)(6)(7, 8)); // 30 console.log(hof(5)(6)(7)(8)(9)); // 35 ``` + +--- + +Write a function that takes any function a return the same function with caching feature + +```javascript +const add = (a, b) => a + b; + +const modifiedAdd = your_function(add); + +modifiedAdd(1,2) /* will calculate 1 + 2 */ +modifiedAdd(5,6) /* will calculate 5 + 6 */ + +modifiedAdd(1,2) /* already called this function with (1,2) arguments so it will not calculate again for 1 and 2, function will directly return answer from the cache i.e. 3.