In the below piece of Trampoline code, I will be calling
onclick = export(add,5)
from my button present in my view. How do I ensure that this call always returns the value of 5
without uncommenting the line where //x=0
in the code below?
var x = 0;
function repeat(operation, num) {
return function () {
if (num <= 0) {
console.log(x);
// x=0;
return;
}
operation();
return repeat(operation, --num);
}
}
function trampoline(fn) {
while (fn && typeof fn === 'function') {
fn= fn();
}
}
this.export = function (operation, num) {
trampoline(function () {
return repeat(operation, num);
});
}
function add()
{
++x;
}
Basically, I wanted a solution where the scope of my variable x will ensure the program always returns the same output when executed n number of times (in other words, I want to set 'export' as a pure function)