Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 809 Bytes

clampNumber.md

File metadata and controls

26 lines (21 loc) · 809 Bytes
title tags author_title author_url author_image_url description image
clampNumber
math,beginner
Deepak Vishwakarma
Implementation of "clampNumber" in typescript, javascript and deno.

JS

Clamps num within the inclusive range specified by the boundary values a and b.

If num falls within the range, return num. Otherwise, return the nearest number in the range.

const clampNumber = (num, a, b) =>
  Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
clampNumber(2, 3, 5); // 3
clampNumber(1, -1, -5); // -1