SJW/demo/main.js

47 lines
1 KiB
JavaScript

window.addEventListener('load', function() {
var modules = {};
modules['Memoizator'] = (function(modules) {
return {
memoize: memoize
};
function memoize(f) {
var cache = {};
return function(n) {
if(cache[n] == undefined) {
cache[n] = f(n);
}
return cache[n];
};
}
})();
modules['Math.Fibonacci'] = (function(memoize, modules) {
var get = memoize(function(n) {
if(n < 2) {
return 1;
} else {
return get(n-1) + get(n-2);
}
});
return {
get: get
};
})(modules['Memoizator'].memoize);
(function(nth, modules) {
var input = document.getElementById('n');
var latestKnownValidInput = input.value;
var result = document.getElementById('result');
input.addEventListener('change', function() {
var newValue = input.value;
if(isNaN(newValue) || newValue < 0 || newValue.trim().length == 0) {
input.value = latestKnownValidInput;
}
result.textContent = nth(input.value);
});
})(modules['Math.Fibonacci'].get);
});