Category : memoization

This is a follow-up to this question asked on Stack Overflow. Write a function ‘howSum(targetSum, numbers)’ that takes in a targetSum and an array of numbers as arguments. The function should return an array containing any combination of elements that add up to exactly the targetSum. If there is no combination that adds up to ..

Read more

I am currently reading up on functional programming for JavaScript and came across the concept of referential transparency. If I understood it correctly it means that a function which is referentially transparent can be converted to its equivalent expression and the program will behave in the same way. This means that the compiler can use ..

Read more

I’m reading up on JavaScript’s virtual getter using Mozilla’s documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get In it there’s a section with some example code: In the following example, the object has a getter as its own property. On getting the property, the property is removed from the object and re-added, but implicitly as a data property this time. Finally, ..

Read more

I am creating a grid of HTML elements which each correspond to a ‘Node’ react component and are held within a parent ‘Visualiser’ react component. Code is seen below. A 2D array of objects holding each Node’s data is created with useState in the Visualiser parent component. Data about a specific Node in this list ..

Read more

This is a valid JavaScript program to generate the Fibonacci number, implemented with memoization. function memoize(fn) { cache = {}; return function(…args){ if (cache[args]) { return cache[args]; } const results = fn.apply(null, args); cache[args] = results; return results; } } function slowfib(n) { if (n < 2) { return n; } return fib(n-1) + fib(n-2); ..

Read more