diff --git a/demo/index.html b/demo/index.html new file mode 100644 index 0000000..6ff5178 --- /dev/null +++ b/demo/index.html @@ -0,0 +1,12 @@ + + + + + SJW demo + + + +

Fibonacci calculator

+

The th number in Fibonacci's sequence is 8

+ + diff --git a/demo/main.js b/demo/main.js new file mode 100644 index 0000000..8d66b70 --- /dev/null +++ b/demo/main.js @@ -0,0 +1,47 @@ +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); +}); diff --git a/demo/src/Main.js b/demo/src/Main.js new file mode 100644 index 0000000..c3ccc10 --- /dev/null +++ b/demo/src/Main.js @@ -0,0 +1,13 @@ +import get as nth from Math.Fibonacci; + +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); +}); diff --git a/demo/src/Math/Fibonacci.js b/demo/src/Math/Fibonacci.js new file mode 100644 index 0000000..3a07923 --- /dev/null +++ b/demo/src/Math/Fibonacci.js @@ -0,0 +1,13 @@ +import memoize from Memoizator; + +var get = memoize(function(n) { + if(n < 2) { + return 1; + } else { + return get(n-1) + get(n-2); + } +}); + +return { + get: get +}; diff --git a/demo/src/Memoizator.js b/demo/src/Memoizator.js new file mode 100644 index 0000000..246595c --- /dev/null +++ b/demo/src/Memoizator.js @@ -0,0 +1,13 @@ +return { + memoize: memoize +}; + +function memoize(f) { + var cache = {}; + return function(n) { + if(cache[n] == undefined) { + cache[n] = f(n); + } + return cache[n]; + }; +}