JSFiddle - React, Tailwind, and code Playground

by jrab227

JavaScript 1.7

const mergeRec = (a, b) => ({
  	value: a.value + b.value,
    totals: a.totals + b.totals
});

const fibDup = (n, totals) =>
	(n === 0 || n === 1) ? ({value: n, totals}) :
  mergeRec(fibNonDup(n-1, totals + 1), fibNonDup(n-2, totals + 1));
  
const fibMem = (n, totals, cache) => {
	if (cache[n]) return {value: cache, totals};
  if (n === 0 || n === 1) return ({value: cache, totals});
  const left = fibNonDup(n, totals+1, cache);
  cache[n-1] = left.value[n-1] + left.value[n-2];
  const right = fibNonDup(n-1, totals+1, cache);
  cache[n] = left.value[n-1] + left.value[n-2];
  return fibNonDup(n-1, totals + 1, cache), fibNonDup(n-2, totals + 1))
  
}