javaScript dynamic programming

  dynamic-programming, javascript

I am learning how to use memoization and I have this problem

const memo = {
  0: 0,
  1: 0,
  2: 1
};

function tribonacci(n, memo) {
  // DO NOT CHANGE THE NEXT FOUR LINES
  if (n in memo) return memo[n];
  const n1 = tribonacci(n - 1)
  const n2 = tribonacci(n - 2)
  const n3 = tribonacci(n - 3)
  // DO NOT CHANGE THE PREVIOUS FOUR LINES

  // Your code here
  if (n === 1 || n === 2) return 1;
  memo[n] = n1 + n2 + n3;
  return memo[n];
}

console.log(tribonacci(3));

But it gives me 2 errors. It says "cannot use in operator of 3 in undefined" and "cannot use in operator to search for 30 in undefined" and I can’t figure out what’s wrong.

Source: Ask Javascript Questions

LEAVE A COMMENT