The tea shop's image server keeps every photo as AVIF, WebP, and JPEG, and sends each browser the best one it can show. Write negotiateImageFormat(accept). Given a request's Accept header, or undefined when the request has none, return "image/avif", "image/webp", or "image/jpeg", or null when the browser accepts none of them.
Read the header the way RFC 9110 says. It's a comma-separated list of media ranges: a type like image/webp, image/* for any image, or */* for anything, each with optional parameters after semicolons. The q parameter (q or Q) is a weight from 0 to 1, and 1 when it isn't there; q=0 means not acceptable. Types are compared in any case, and spaces around commas and semicolons don't matter.
A format's weight comes from the most specific range that covers it: image/webp beats image/*, which beats */*. So image/*;q=0.8, image/webp;q=0 rules WebP out and gives every other image 0.8. Pick the format with the highest weight above 0. When formats tie, use the server's own order of preference: AVIF, then WebP, then JPEG, which is usually smallest file first. A request without an Accept header accepts anything, so it gets AVIF.
When no format has a weight above 0, return null. RFC 9110 lets the server answer that with 406 Not Acceptable, or ignore the header and send what it has.
// The Accept header Chrome sends for an image
negotiateImageFormat("image/jxl,image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"); // "image/avif"
negotiateImageFormat("image/webp,*/*;q=0.8"); // "image/webp": AVIF only gets 0.8
negotiateImageFormat("image/avif;q=0, image/*"); // "image/webp"
expect(negotiateImageFormat("image/jxl,image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8")).toBe("image/avif");
expect(negotiateImageFormat("image/webp,image/avif;q=0.5,*/*;q=0.1")).toBe("image/webp");
expect(negotiateImageFormat("image/jpeg, image/webp;q=0.9")).toBe("image/jpeg");
expect(negotiateImageFormat("image/avif;q=0.6, image/webp;q=0.7, image/jpeg;q=0.65")).toBe("image/webp");
expect(negotiateImageFormat("image/webp,*/*;q=0.8")).toBe("image/webp");
expect(negotiateImageFormat("image/jpeg, image/webp, image/avif")).toBe("image/avif");
expect(negotiateImageFormat("image/jpeg,image/webp")).toBe("image/webp");
expect(negotiateImageFormat("image/jpeg;q=0.5, image/webp;q=0.5")).toBe("image/webp");
expect(negotiateImageFormat("image/*")).toBe("image/avif");
expect(negotiateImageFormat("*/*")).toBe("image/avif");
expect(negotiateImageFormat("image/avif;q=0, image/*")).toBe("image/webp");
expect(negotiateImageFormat("image/*;q=0.8, image/webp;q=0, image/avif;q=0")).toBe("image/jpeg");
expect(negotiateImageFormat("image/webp;q=0")).toBe(null);
expect(negotiateImageFormat("image/*;q=0.5, image/webp")).toBe("image/webp");
expect(negotiateImageFormat("image/*, image/avif;q=0.3")).toBe("image/webp");
expect(negotiateImageFormat("*/*;q=0.9, image/*;q=0.2, image/jpeg")).toBe("image/jpeg");
expect(negotiateImageFormat("image/jpeg;q=0.4, */*;q=0.5")).toBe("image/avif");
expect(negotiateImageFormat(undefined)).toBe("image/avif");
expect(negotiateImageFormat()).toBe("image/avif");
expect(negotiateImageFormat("text/html,application/xhtml+xml")).toBe(null);
expect(negotiateImageFormat("image/png,image/gif")).toBe(null);
expect(negotiateImageFormat("*/*;q=0")).toBe(null);
expect(negotiateImageFormat("IMAGE/WEBP ; Q=0.7 , image/JPEG;q=0.6")).toBe("image/webp");
expect(negotiateImageFormat(" image/avif ;q=0.9,image/webp")).toBe("image/webp");
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Negotiate an image format. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.