The situation
You own the My orders page of an online shop. It lists a customer's past orders five at a time, with Previous and Next buttons. The list comes from a small helper, paginate, which the backend team also plans to reuse for the orders API, where callers choose their own page size.
Two reports have landed on your desk:
"Page 1 of my order history doesn't show my most recent orders. I have to click Next several times to find what I bought yesterday, and Next never greys out, even after the last order."
From the backend lead: "If someone asks for 10,000 orders per page, paginate will happily return them all. That has to be capped before we reuse it in the API."
Your tasks
Task 1: Fix paginate
Fix paginate(items, page, limit) in src/utils/paginate.js so that:
- Pages are numbered from 1, and page 1 holds the most recent orders (newest
createdAt first). - The array passed in is not reordered. Sort a copy.
limit is 10 when it isn't given, and never more than 50. Use the DEFAULT_LIMIT and MAX_LIMIT constants.- The result is
{ items, page, limit, total }, where total is the number of orders across all pages. - A page past the end has an empty
items list.
Make every test in src/utils/paginate.test.js pass.
Task 2: Check the page
In the preview, page 1 should start with Order #23 (the newest). Next and Previous should walk through all 23 orders, and Next should be disabled on the last page (page 5).
Project files
package.json
public/
index.html
src/
index.js
index.css
App.js
App.css
data/
orders.js
utils/
paginate.js
paginate.test.jssrc/App.js: the My orders page. It keeps the current page number, asks paginate for 5 orders at a time and uses total to decide when Next is disabled.src/App.css: one custom rule that keeps order numbers lined up. Everything else is styled with Tailwind classes.src/data/orders.js: 23 sample orders, each with an id and a createdAt timestamp. A higher id is a more recent order.src/utils/paginate.js: the paging helper and its DEFAULT_LIMIT / MAX_LIMIT constants.src/utils/paginate.test.js: the tests for paginate.package.json, public/index.html, src/index.js and src/index.css: the standard React app setup. src/index.js starts the app and loads Tailwind CSS, and src/index.css holds the global styles. You don't need to change these.
How to check your work
- Tests: press ▶ Run tests next to the Tests heading in the bottom panel. They don't re-run on their own, so run them again after each change. PASS means every visible test passed.
- Preview: the bottom-left panel shows the running app and rebuilds as you type. Use Open in new tab above it, or Copy link and paste it into a new tab, to see it full size. That tab follows your edits.
- Your work is saved in this browser. Reset (top right) puts the starting code back.