Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 642 Bytes

check-if-a-number-is-in-a-given-range.mdx

File metadata and controls

28 lines (22 loc) · 642 Bytes
category created title updated
Validator
2020-06-04
Check if a number is in a given range
2021-10-13

JavaScript version

const inRange = (num, a, b, threshold = 0) => Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold;

TypeScript version

const inRange = (num: number, a: number, b: number, threshold: number = 0): boolean =>
    Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold;

Examples

inRange(10, 5, 15); // true
inRange(10, 5, 6); // false
inRange(10, 15, 5); // true
inRange(-10, -5, -15); // true