Update clampNumber.md
This commit is contained in:
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
Clamps `num` within the inclusive `lower` and `upper` bounds.
|
Clamps `num` within the inclusive `lower` and `upper` bounds.
|
||||||
|
|
||||||
If `lower` is greater than `upper`, swap them. If `num` falls within the range, return `num`. Otherwise return the nearest number in the range.
|
If `lower` is greater than `upper`, swap them.
|
||||||
|
If `num` falls within the range, return `num`.
|
||||||
|
Otherwise return the nearest number in the range.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const clampNumber = (num, lower, upper) => {
|
const clampNumber = (num, lower, upper) => {
|
||||||
if(lower > upper) upper = [lower, lower = upper][0];
|
if(lower > upper) upper = [lower, lower = upper][0];
|
||||||
return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper)
|
return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper)
|
||||||
}
|
}
|
||||||
|
|
||||||
// clampNumber(2, 3, 5) -> 3
|
// clampNumber(2, 3, 5) -> 3
|
||||||
|
|||||||
Reference in New Issue
Block a user