Number
_.inRange
Checks if n is between start and up to, but not including, end. If end is not specified, it's set to start with start then set to 0. If start is greater than end the params are swapped to support negative ranges.
- // Lodash
- _.inRange(3, 2, 4);
- // output: true
- _.inRange(-3, -2, -6);
- // output: true
- //Native
- const inRange = (num, init, final) => {
- if(final === undefined){
- final = init;
- init = 0;
- }
- return (num >= Math.min(init, final) && num < Math.max(init, final));
- }
- inRange(3, 2, 4);
- // output: true
- inRange(-3, -2, -6);
- // output: true
Browser Support for Math.min() and Math.max()
✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .