Skip to content
DevFlash

Junior

Counter with limits and step

Small component, real requirements: limits, step, disabled states and reset.

FreeJunior~20 min

This challenge is under editorial review.

Why it shows up in interviews

The counter looks trivial, which is exactly why it opens so many front-end interviews. In ten minutes it shows whether you read the requirements, where you enforce a rule and whether rapid clicks still produce the right value.

Most candidates get the happy path. The grade comes from the limits: clamping instead of checking after the fact, disabling the button at each edge so assistive technology knows, and using the functional state update when several clicks land in a row.

What you will build

  1. Show the value in an element with data-testid="count".
  2. Buttons named Increment and Decrement move by step (default 1).
  3. Clamp to [min, max] and disable the button at each limit.
  4. Every click counts, even several in a row.
  5. Reset returns to the initial value.

Out of scope: Styling. The tests only look at behavior and accessible names.

How you will be graded

Passing the tests is the start. In the follow-up conversation, a good explanation covers:

  • Explains where the limit is enforced and why there.
  • Mentions the functional state update and when it matters.
  • Covers accessibility of the buttons.

Starter code

/src/Counter.tsx

type CounterProps = {
min: number;
max: number;
step?: number;
initial?: number;
};
export function Counter({ min, max, step = 1, initial = min }: CounterProps) {
// implemente aqui
return (
<div>
<button type="button">-</button>
<output>{initial}</output>
<button type="button">+</button>
</div>
);
}
See the tests that will run /src/Counter.test.tsx
import "@testing-library/jest-dom";
import { render, screen, fireEvent } from "@testing-library/react";
import { Counter } from "./Counter";
const count = () => screen.getByTestId("count");
const inc = () => screen.getByRole("button", { name: "Increment" });
const dec = () => screen.getByRole("button", { name: "Decrement" });
describe("Counter", () => {
it("starts at the initial value", () => {
render(<Counter min={0} max={10} initial={4} />);
expect(count()).toHaveTextContent("4");
});
it("moves by the given step", () => {
render(<Counter min={0} max={10} step={2} />);
fireEvent.click(inc());
fireEvent.click(inc());
expect(count()).toHaveTextContent("4");
});
it("never goes above max, even when the step overshoots", () => {
render(<Counter min={0} max={10} step={3} initial={9} />);
fireEvent.click(inc());
expect(count()).toHaveTextContent("10");
expect(inc()).toBeDisabled();
});
it("never goes below min", () => {
render(<Counter min={0} max={10} step={5} initial={2} />);
fireEvent.click(dec());
expect(count()).toHaveTextContent("0");
expect(dec()).toBeDisabled();
});
it("counts every click, even in a quick burst", () => {
render(<Counter min={0} max={100} />);
fireEvent.click(inc());
fireEvent.click(inc());
fireEvent.click(inc());
expect(count()).toHaveTextContent("3");
});
it("reset goes back to the initial value", () => {
render(<Counter min={0} max={10} initial={1} />);
fireEvent.click(inc());
fireEvent.click(screen.getByRole("button", { name: "Reset" }));
expect(count()).toHaveTextContent("1");
});
});

Hints

Open only if you get stuck. Each one gives away a little more.

Hint 1

Write a tiny clamp(value, min, max) helper and run every new value through it.

Hint 2

setValue(v => ...) reads the latest state. setValue(value + step) reads the value from the last render.

References