Spot the bug - #17

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.

MechaPrime

Map doesn’t work with cache[id] — that’s just setting a random property on the object, so the “cache” check won’t behave like you think (and 0/empty ids get weird too). Use cache. has(id) + cache. get(id) and cache. set(id, user) instead.