sizes tells the browser how wide an image will be before any CSS has loaded, so it can choose from srcset as soon as it sees the <img>. Write evaluateSizes(sizes, viewportWidth). Given a sizes value (or null for an image without one) and the viewport's width in CSS pixels, return the image's width in CSS pixels, the way the HTML Standard's "parse a sizes attribute" works it out.
Split the value on commas into entries, trim each one, and go through them in order. An entry is a media condition followed by a size, like (max-width: 600px) 100vw, or a size on its own, like 50vw. The first entry whose condition is true gives the width. An entry without a condition counts as true: the standard says only the last entry should be one, but it takes a bare size wherever it finds one, so nothing after it is read.
A size is a number (like 600, 33.5, or .5) and a unit: px, vw (a hundredth of the viewport's width), or em, which is 16 px here: in sizes and in media queries, em is the browser's default font size, not the page's. 0 with no unit is a size too. Anything else where the size should be, such as a percentage, a negative length, another number without a unit, or a word, makes the entry invalid, and so does an empty entry; the standard skips an invalid entry and goes on to the next. Skip auto as well: it's for a lazy-loaded <img> that has been laid out, and here there's no image.
A condition is one or more of (max-width: L) and (min-width: L), joined by and, where L is a number in px or em. max-width is true when the viewport is L wide or narrower, and min-width when it's L or wider; with and, every part has to be true. Spaces inside the parentheses are optional. The tea shop's templates use no other conditions, so treat anything else as a condition that doesn't parse, and skip the entry. A condition that starts with a media type, like screen and (min-width: 600px) or print, never parses in a browser either: sizes takes a media condition, not a whole media query.
When no entry gives a width, the standard's answer is 100vw, the whole viewport. That's also the answer when sizes is null, empty, or only spaces.
const sizes = "(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 400px";
evaluateSizes(sizes, 375); // 375: a phone, where the photo is as wide as the screen
evaluateSizes(sizes, 900); // 450: two photos side by side
evaluateSizes(sizes, 1440); // 400: the last entry has no condition
const sizes = "(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 400px";
expect(evaluateSizes(sizes, 375)).toBe(375);
expect(evaluateSizes(sizes, 900)).toBe(450);
expect(evaluateSizes(sizes, 1440)).toBe(400);
expect(evaluateSizes("(max-width: 600px) 100vw, 50vw", 600)).toBe(600);
expect(evaluateSizes("(max-width: 600px) 100vw, 50vw", 601)).toBe(300.5);
expect(evaluateSizes("(min-width: 1024px) 300px, 100vw", 1024)).toBe(300);
expect(evaluateSizes("(min-width: 1024px) 300px, 100vw", 1023)).toBe(1023);
expect(evaluateSizes("(min-width:1024px) 300px, 100vw", 1100)).toBe(300);
const sizes = "(min-width: 600px) and (max-width: 1023px) 50vw, 300px";
expect(evaluateSizes(sizes, 800)).toBe(400);
expect(evaluateSizes(sizes, 500)).toBe(300);
expect(evaluateSizes(sizes, 1100)).toBe(300);
expect(evaluateSizes("(min-width: 40em) 20em, 100vw", 640)).toBe(320);
expect(evaluateSizes("(min-width: 40em) 20em, 100vw", 639)).toBe(639);
expect(evaluateSizes("(max-width: 37.5em) .5em, 0", 600)).toBe(8);
expect(evaluateSizes("50vw, (max-width: 600px) 100vw", 400)).toBe(200);
expect(evaluateSizes("(max-width: 600px) 100vw, 50vw,", 800)).toBe(400);
expect(evaluateSizes("(max-width: 600px) 300px", 800)).toBe(800);
expect(evaluateSizes("", 800)).toBe(800);
expect(evaluateSizes(" ", 800)).toBe(800);
expect(evaluateSizes(null, 800)).toBe(800);
const sizes = "(max-width: 600px) 50%, (max-width: 600px) -10px, (max-width: 600px) 100, 50vw (max-width: 600px), (max-width: 600px), , 320px";
expect(evaluateSizes(sizes, 500)).toBe(320);
expect(evaluateSizes("(max-width: 600px) 0, 100vw", 500)).toBe(0);
const sizes = "screen and (max-width: 600px) 100vw, print 50vw, max-width: 600px 25vw, 300px";
expect(evaluateSizes(sizes, 500)).toBe(300);
expect(evaluateSizes("auto, (max-width: 600px) 100vw, 50vw", 400)).toBe(400);
expect(evaluateSizes("auto, 300px", 900)).toBe(300);
expect(evaluateSizes("auto", 900)).toBe(900);
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 Evaluate a sizes attribute. 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.