← All exercises

The avatar that crashes on an empty name

An avatar component in a team dashboard · difficulty 1/3 · ~5 min

A teammate asked the AI assistant: "Write an `initials(fullName)` helper for the avatar component. One word gives one letter, two or more words give the first letter of the first and last word, always uppercase. Add an `avatarInitials(user)` wrapper that works on our user records, where `firstName` and `lastName` are both optional because some accounts were imported from an invite link with only an email." The code came back with tests, and the avatars look right on the demo team. The contract promised by the docstring: - Words are separated by whitespace. - `"Ada"` gives `"A"`, `"Ada King Lovelace"` gives `"AL"`. - An empty or whitespace-only name gives `""`, so the component can render a placeholder. - `avatarInitials` accepts a record where either name part is missing. Review it before it ships to the members page, which lists every account, including the imported ones.
Repair the code until the hidden tests pass.

code.ts

Visible tests (they pass on the code as given)
import { describe, it, expect } from "@debunk/harness";
import { initials, avatarInitials } from "./code";

describe("initials", () => {
  it("takes the first letter of the first and last word", () => {
    expect(initials("Ada Lovelace")).toBe("AL");
    expect(initials("Ada King Lovelace")).toBe("AL");
  });

  it("uppercases a single-word name", () => {
    expect(initials("ada")).toBe("A");
  });

  it("avatarInitials joins first and last name", () => {
    expect(avatarInitials({ firstName: "Grace", lastName: "Hopper" })).toBe("GH");
  });
});