Javascript Function Increment/Decrement Number with Min Max Number
Javascript Function to Increment and/or Decrement a Number, Taking into Consideration a Minimum Allowed Number and Maximum Allowed Number
Home
Short:
/* ----------------------------------------------------- function to increment and/or decrement a number ensuring the number doesn't go below a minimum and above a maximum [num] the number to increment/decrement [bincrement] boolean - increasing or decreasing [incremdecremval] - amount of number increase/decrease [min] minimum number [max] maximum number ----------------------------------------------------- */ function incrementNumMinMax(num, bincrement, incremdecremval, min, max){ // increment or decrement [num] based on if [bincrement] = true or false num += (bincrement == false) ? -incremdecremval : incremdecremval; // js function to ensure min and max value num = Math.min(Math.max(num, min), max) return num; }
source code home