Write rate(name, value). name is one of the five metrics the web-vitals library rates: "LCP", "INP", "CLS", "FCP", or "TTFB". value is what a page measured: milliseconds for the four timings, and a score for CLS. Return the rating web-vitals gives that value, as the strings it uses: "good", "needs-improvement", or "poor".
Each metric has two thresholds. A value up to the first is good, a value up to the second needs improvement, and a value above the second is poor, so a value right on a threshold gets the better rating. Use the thresholds web-vitals uses; the first hint has them if you don't know them by heart.
Throw an Error whose message is Unknown metric "<name>" for any other name. That includes "FID": web-vitals no longer measures it, since INP replaced it as the Core Web Vital for responsiveness. Throw a RangeError when value isn't a finite number of 0 or more.
rate("LCP", 1800); // "good": the tea shop's home page
rate("INP", 350); // "needs-improvement"
rate("CLS", 0.31); // "poor"
rate("LCP", 2500); // "good": right on the threshold
rate("FID", 40); // throws Error: Unknown metric "FID"
expect(rate("LCP", 1200)).toBe("good");
expect(rate("LCP", 3100)).toBe("needs-improvement");
expect(rate("LCP", 5200)).toBe("poor");
expect(rate("LCP", 2500)).toBe("good");
expect(rate("LCP", 4000)).toBe("needs-improvement");
expect(rate("INP", 200)).toBe("good");
expect(rate("INP", 500)).toBe("needs-improvement");
expect(rate("TTFB", 1800)).toBe("needs-improvement");
expect(rate("INP", 201)).toBe("needs-improvement");
expect(rate("INP", 501)).toBe("poor");
expect(rate("FCP", 1800)).toBe("good");
expect(rate("FCP", 1801)).toBe("needs-improvement");
expect(rate("FCP", 3001)).toBe("poor");
expect(rate("TTFB", 800)).toBe("good");
expect(rate("TTFB", 1200)).toBe("needs-improvement");
expect(rate("TTFB", 1801)).toBe("poor");
expect(rate("CLS", 0)).toBe("good");
expect(rate("CLS", 0.1)).toBe("good");
expect(rate("CLS", 0.11)).toBe("needs-improvement");
expect(rate("CLS", 0.25)).toBe("needs-improvement");
expect(rate("CLS", 0.26)).toBe("poor");
expect(rate("CLS", 3)).toBe("poor");
expect(() => rate("FID", 40)).toThrow('Unknown metric "FID"');
expect(() => rate("lcp", 1200)).toThrow('Unknown metric "lcp"');
expect(() => rate("toString", 1)).toThrow('Unknown metric "toString"');
expect(() => rate("LCP", -1)).toThrow(RangeError);
expect(() => rate("LCP", NaN)).toThrow(RangeError);
expect(() => rate("LCP", "2500")).toThrow(RangeError);
expect(() => rate("CLS", Infinity)).toThrow(RangeError);
expect(rate("TTFB", 0)).toBe("good");
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 Rate a metric. 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.