Exercise on Proxies

Using your knowledge of proxies object, you must use them to: i) log the number of times a function is accessed ii) create a revocable discount object

Exercise 1:

Suppose the following fibonacci implementation is given:

fibonacci = n => 
    n <= 1 ? n : 
    fibonacci( n - 1 ) + fibonacci( n - 2 );

Determine how many times the fibonacci function is called when evaluating fibonacci( 12 ).

Determine how many times fibonacci is called with the argument 2 when evaluating fibonacci( 12 ).

Press + to interact
let fibonacci = n =>
n <= 1 ? n : fibonacci( n - 1 ) + fibonacci( n - 2 ); //the fibonacci function
let fibCalls = 0; // total calls to the fibonacci function
let fibCallsWith2 = 0; // calls to the fibonacci function with 2 as an argument
//Write your code here
// Do not edit code below this line
console.log( 'fibCalls:', fibCalls );
console.log( 'fibCallsWith2:', fibCallsWith2 );

Explanation:

The solution is not that hard. As we learned earlier, fibonacci = new Proxy( fibonacci, { uses the same reference name as our fibonacci function ...