Spot the bug - #18

There is one deliberate bug here.

const cache = new Map();

function getUser(id) {
  if (cache[id]) return cache[id];
  const user = { id, name: 'Ari' };
  cache[id] = user;
  return user;
}

Reply with what is broken and how you would fix it.

Sarah

cache is a Map, but the code is treating it like an object with cache[id], so the “cache hit” path never really uses the Map API.

Fix by either switching to cache.has(id) / cache.get(id) / cache.set(id, user), or change cache to {} if you actually wanted property access.

Yep