Skip to content

Search page URL

Web / frontend · Difficulty 1/5 · TypeScript · 23 linesOrigin: Fictional

Task

What the AI was asked to do

Build the search page URL from the product search form's filters. term is the text the user typed, category is a short name from a fixed list (lowercase letters and hyphens only), page is an integer starting at 1. base is a path without a question mark (/search). Leave out empty fields and page 1; trim spaces around the search text.

Handoff note

What the AI wrote when it handed over the code:

“Empty fields are skipped, the search text is trimmed and page 1 is left out of the URL. Every filter gets its own parameter.”

The code the AI wrote

TypeScript23 lines

Mark the lines you think are wrong; give each mark a severity and a one-sentence reason.

Click or tap a line. With a keyboard, move to the line and press Enter.

0 of 5 marks
How to pick a severity
  • CriticalData loss, a security hole or lost money
  • MajorThe user gets a wrong result in a case the task asks for
  • MinorA rare edge case outside the task, or readability

In the Testing topic, severity is set by the effect of the broken implementation the test doesn't catch.

Your reason isn't graded; you'll compare it with a sample answer.

export interface SearchFilters {
  term: string;
  category: string;
  page: number;
}
6
export function buildSearchUrl(base: string, filters: SearchFilters): string {
  const params: string[] = [];
  const term = filters.term.trim();
  if (term !== "") {
    params.push(`q=${term}`);
  }
  if (filters.category !== "") {
    params.push(`category=${filters.category}`);
  }
  if (filters.page > 1) {
    params.push(`page=${filters.page}`);
  }
  if (params.length === 0) {
    return base;
  }
  return `${base}?${params.join("&")}`;
}

The test that proves it

The right option is the test that fails on this code, which means it exposes the bug.

Test options
Mark at least one line to submit.

Submitting sends seven pieces of information to a counter; your reason text stays on your device. Data