Skip to main content
🟨 JavaScript Foundations

Numbers & Math

JavaScript does math with the symbols you already know: + add, - subtract, * multiply, / divide.

3 + 4;   // 7
10 / 2;  // 5

It also has a Math object full of helpers: Math.min, Math.max, Math.round, Math.floor. A common need is to clamp a value into a range β€” never below a minimum, never above a maximum.

Math.max(0, Math.min(10, value)); // keep value within 0..10

Read that inside-out: Math.min(10, value) caps the top at 10, then Math.max(0, …) lifts the bottom up to 0.

Your task

Write clamp(n, min, max) that returns n limited to the range [min, max].

⚑ 25 XP

🧱 Code blocks

Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.

solution.jsRuns in your browser
Loading editor…
Tests3 to pass
in range
below min
above max

Run your code to check these.