← All exercises

The leaderboard where 9 beats 10

A leaderboard in a fitness app · difficulty 1/3 · ~5 min

A teammate asked the AI assistant: "Write the leaderboard helpers for the weekly challenge: `topScores(scores, n)` for the top-N list, `personalBest(scores)` for the profile card, and `rankOf(scores, score)` for the 'you are #3 this week' badge. Scores are numbers of points; they can be fractional. Don't mutate the input." The code came back short, readable, with a shared helper and passing tests. Contract from the docstrings: - `topScores` returns the `n` highest scores, highest first; all of them if there are fewer than `n`. - `personalBest` returns the highest score, or `null` when there are none. - `rankOf` returns the 1-based rank of a score (1 is the best), or `null` if the score is not on the board. - None of the helpers mutates `scores`. The tests are green. Check whether the helpers are right for the scores a real user would have.

code.ts

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

describe("leaderboard", () => {
  it("topScores returns the highest scores first", () => {
    expect(topScores([3, 1, 2, 5, 4], 3)).toEqual([5, 4, 3]);
  });

  it("personalBest returns the highest score", () => {
    expect(personalBest([2, 9, 4])).toBe(9);
  });

  it("rankOf gives rank 1 to the best score", () => {
    expect(rankOf([7, 3, 8], 8)).toBe(1);
    expect(rankOf([7, 3, 8], 3)).toBe(3);
  });
});