JavaScript traps · Chapter 3 of 16
Sort without a compare function
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Everyday traps
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: put masala chai at the bottom on purpose
Write reproduce() in repro.js. Build the ticket's menu, an array of { name, cups } in this order: masala chai 112, cutting chai 64, bun maska 48, ginger chai 30, parle-g 25 (the names exactly as written here). Call topSellers(menu, 5) from board.js and resolve to the names it gives.
The check runs your reproduce() twice: beside the original board.js it must resolve to [ 'cutting chai', 'bun maska', 'ginger chai', 'parle-g', 'masala chai' ] (masala chai at the bottom), and beside a fixed board.js it must resolve to [ 'masala chai', 'cutting chai', 'bun maska', 'ginger chai', 'parle-g' ]. So it must really call topSellers with that menu. No real waiting: reproduce() must finish within 2 seconds.
- Task 2
board.js: most cups first, and leave the menu alone
Fix topSellers(menu, n) in board.js. menu is an array of { name, cups }. It must return the names of the n items with the most cups, most cups first, and items with the same cups in name order (as localeCompare orders them). When n is more than the menu has, it returns every item's name in that order. The menu it is given must not change in any way: the same items in the same order, because the menu screen shares that array.
Keep the log line top sellers: <names, comma separated> as it is. The check uses its own menus, with counts such as 1000, 140, 21, 9 and 5, and looks at the menu after each call; a fix that orders the names right but still reorders the menu is as wrong as the bug.
- Task 3
board.test.js: a test that would have caught it
Write a vitest test file, board.test.js, that would have caught this bug. The check runs your file three times: beside the original board.js, where at least one test must fail; beside the fixed board.js, where every test must pass; and beside a wrong fix that orders the counts as numbers but still sorts the menu in place, where at least one test must fail.
So test both things the ticket found: the names come most cups first, on a menu whose counts sort differently as text (100 and 25, say), and the menu you pass in keeps its own order after the call. Each test must finish within 3 seconds.
What you will learn
- repro.js: put masala chai at the bottom on purpose
- board.js: most cups first, and leave the menu alone
- board.test.js: a test that would have caught it
More in Everyday traps
Build "Sort without a compare function" yourself
Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.