Describe numeric boundaries
Difficulty: Easy
Try your first run
No account is needed to try this exercise. Write your answer, run the tests, and use the feedback to improve it.
- Read the examples and the required return value.
- Edit the supplied function in the editor, keeping its name and parameters. Return your answer, then select Run code.
- Use the test feedback to try again. Hints are available without replacing your code.
Implement clamp(value: number, low: number, high: number): number. Return value when it lies in the inclusive interval from low to high. Return the nearest boundary when it lies outside. Inputs are finite numbers and satisfy low <= high.
What this practices
Clamping limits a value without changing values that are already allowed. It differs from rounding, wrapping around an interval, or converting a value to a percentage.
Work through an example
With bounds 0 and 10, input 5 remains 5, input -1 becomes 0, and input 20 becomes 10. Both boundaries are included: inputs 0 and 10 remain unchanged.
Edge cases to check
- Equal bounds describe a single allowed value; every input returns that boundary.
- Negative bounds are valid.
- Fractions are preserved when inside the interval.
- Values exactly on either bound require no adjustment.
TypeScript details
The three number annotations do not prove that the lower bound precedes the upper bound. That ordering is an explicit input precondition. Do not swap bounds, parse strings, or round the result. TypeScript checks type relationships; runtime comparisons determine which numeric result satisfies the interval.
Before coding, predict clamp(-3, -5, -1) and clamp(8, 2, 2). They should return -3 and 2, respectively.
Examples
5, 0, 105-1, 0, 10020, 0, 1010What this exercise practices
Enforce an inclusive numeric interval without rounding or wrapping. The annotations describe number types; the exercise contract supplies finite values and ordered bounds.
Common mistakes and boundary checks
- Try both exact endpoints and a fraction inside the interval.
- Equal bounds must produce that one allowed value.
- Negative bounds are valid. Do not swap the bounds or convert the result to an integer.
Constraints
Use strict TypeScript and a standalone function. No imports, packages, DOM, network, or file access. Inputs are JSON-compatible with at most 1,000 elements and 100,000 characters. Do not mutate inputs. Return the specified result rather than printing. Medium tasks combine narrowing and typed transformations.
Review lesson: Function signatures and missing resultsA little nudge
Reveal one hint at a time. Give yourself a moment to try it.
- First enforce the lower bound.
- Then enforce the upper bound.
Worked solution
Try your own approach first, then compare the reasoning.
function clamp(value: number, low: number, high: number): number {
return Math.min(high, Math.max(low, value));
}
Math.max(low, value) enforces the lower boundary. Math.min(high, ...) then enforces the upper boundary. With low <= high, this preserves every in-range value and handles equal bounds. Numeric annotations alone cannot validate the interval ordering.
Complexity
O(1) time and O(1) extra space.
Ready when you are
Write your solution, then run it against the practice tests.
Press Esc, then Tab to leave the editor. Code runs in an isolated browser workspace. Tests support learning, not verified grading.