0 of 6 problems solved0%
1 of 6

Number or Text Size

typeof narrows primitive unions at runtime. After a typeof value === 'string' check, you can safely use string-only members like .length. If it’s a number, you can use it directly.

function describe(v: string | number) {
  if (typeof v === "string") return "text:" + v.length;
  return "num:" + v;
}

Return a number so tests can compare easily, and cover an empty string as an edge case.

Your Task

Compute a size number from a string | number.

  • Write function sizeOf(value: string | number): number.
  • If value is a string, return its .length.
  • If value is a number, return the number itself.
  • Handle the empty string correctly (size 0).