Skip to content
dreamcode
dreamcode
Lesson
Practice · Memoization
  1. Predict
  2. Arrange
  3. Fill in
+20 XP on finish
PREDICT · READ BEFORE YOU WRITE

What does this program print?

Trace the program the way the computer would, then make your call before running anything.

const memo = new Map();
const square = (n) => {
  if (memo.has(n)) return "cached";
  memo.set(n, n * n);
  return memo.get(n);
};
console.log(square(4), square(4));