Back to practice

Describe numeric boundaries

Difficulty: Easy

TypeScript

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.

  1. Read the examples and the required return value.
  2. Edit the supplied function in the editor, keeping its name and parameters. Return your answer, then select Run code.
  3. Use the test feedback to try again. Hints are available without replacing your code.

Sign in to save items for review and keep private notes.

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, 100
20, 0, 1010

What 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 results
TypeScript

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.

Reset your code?

This replaces your current code with the starter and stops any running tests.