An AI assistant wrote GET /orders?since=… for the shop: the orders placed on or after a date. Customers type the date day first, like 18/04/2026. The database keeps each order's date year first, like 2026-04-18. The AI compared the two texts as they are:
app.get("/orders", (req: Request, res: Response) => {
const since = req.query.since as string;
const rows = db.prepare("SELECT * FROM orders").all() as Order[];
const result: Order[] = [];
for (const row of rows) {
if (row.ordered >= since) {
result.push(row);
}
}
res.json(result);
});
TypeScript, like JavaScript, compares strings character by character, and the first difference decides. "2026-03-02" >= "18/04/2026" is true, because "2" comes after "1". So since=18/04/2026 answered all 7 orders and since=30/04/2026 answered none. The impossible 31/04/2026 got 200 and an empty list, and so did a request without since. Both sides are of the type string, so tsc saw nothing wrong.
The fix: turn each text into a real date, of the type Date, with new Date(text), then compare dates. The one format new Date reads the same way everywhere is the ISO format: year first, with dashes. It never complains: a text it can't read gives an Invalid Date, whose getTime() is NaN, and a day that doesn't exist quietly rolls over into the next month. Turning the date back into text shows it:
const d = new Date("2026-05-07");
console.log(d.toISOString().slice(0, 10)); // 2026-05-07
const bad = new Date("2026-02-30"); // there is no 30 February
console.log(bad.toISOString().slice(0, 10)); // 2026-03-02
console.log(isNaN(new Date("soon").getTime())); // true: an Invalid Date
console.log("07/05/2026".split("/")); // [ '07', '05', '2026' ]
The requests and the answers your route must give:
GET /orders?since=18/04/2026 → 200 [{"customer_id": 3, "id": 4, "ordered": "2026-04-18", "status": "shipped"}, …] (orders 4 to 7)
GET /orders?since=30/04/2026 → 200 [{"customer_id": 4, "id": 5, "ordered": "2026-05-07", "status": "paid"}, …] (orders 5 to 7)
GET /orders?since=01/06/2026 → 200 []
GET /orders?since=31/04/2026 → 400 {"error": "since must be a date like 18/04/2026"}
GET /orders → 400 {"error": "since must be a date like 18/04/2026"}
Your task: rewrite the function of the /orders route. Split since at "/", join the year, the month and the day with dashes, and read that text with new Date. Answer 400 with the error when it isn't a real date. Read each order's ordered with new Date as it is, and answer the orders placed on or after since.
Assignment
Follow the Coding Tutorial to rewrite the /orders route so it compares real dates instead of texts.
Hintnew Date never throws, so check the date yourself: isNaN(since.getTime()) is true for an Invalid Date (isNaN takes a number, so pass it getTime()), and since.toISOString().slice(0, 10) differs from the text you built when the day doesn't exist. Read since as String(req.query.since || ""): String gives a string, which has .split, and a missing date lands in the same check. Use >= so an order placed on the since day itself is kept.
Free plan: 30 tutor messages. PRO is unlimited.
