A screenshot service can only render at the viewport widths in widths, which are listed in no particular order. A user asks for a screenshot at width target.
Write nearestWidth(widths, target) that returns the supported width closest to target. If two widths are equally close, return the smaller one.
Input: widths = [1280,375,768,1024], target = 800 Output: 768
768 is 32 away from 800. 1024 is 224 away.
Input: widths = [480,320], target = 400 Output: 320
Both are 80 away, so pick the smaller width, 320.
1 <= widths.length <= 10001 <= widths[i], target <= 10000Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #154 Nearest Supported Viewport. 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.