Scriptdrill

Level 1: Basics Exercise 8 of 45 Pro

Clamp a number

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

Return `n` limited to the range from `min` to `max`, inclusive: `min` if `n` is below it, `max` if `n` is above it, otherwise `n` itself. You can assume `min <= max`.

What this exercise teaches

Math.max(n, min) is never below min, and Math.min of that with max is never above max. Values in range pass through unchanged, decimals included.

A hint if you get stuck

`Math.max(n, min)` lifts low values; wrap it in `Math.min(..., max)` to cap high ones.

This exercise is part of Pro

Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.

You create a free account first, so Pro is attached to it.

Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.

Start with the free exercises

More exercises in Basics