Page 85 - JavaScript
P. 85
console.log(min); // Logs: 1
Restrict Number to Min/Max Range
If you need to clamp a number to keep it inside a specific range boundary
function clamp(min, max, val) {
return Math.min(Math.max(min, +val), max);
}
console.log(clamp(-10, 10, "4.30")); // 4.3
console.log(clamp(-10, 10, -8)); // -8
console.log(clamp(-10, 10, 12)); // 10
console.log(clamp(-10, 10, -15)); // -10
Use-case example (jsFiddle)
Getting roots of a number
Square Root
Use Math.sqrt() to find the square root of a number
Math.sqrt(16) #=> 4
Cube Root
To find the cube root of a number, use the Math.cbrt() function
6
Math.cbrt(27) #=> 3
Finding nth-roots
To find the nth-root, use the Math.pow() function and pass in a fractional exponent.
Math.pow(64, 1/6) #=> 2
Read Arithmetic (Math) online: https://riptutorial.com/javascript/topic/203/arithmetic--math-
https://riptutorial.com/ 42

