Topic Description

You are developing a system that processes numerical data. Write a function named clamp that takes three parameters:

This function should ensure that the output number always falls within the specified range, including the minimum and maximum values themselves. How would you implement this clamp function?

const clampedValue = clamp(10, 5, 15);
console.log(clampedValue); // Output: 10

const clampedValue2 = clamp(-2, 0, 10);
console.log(clampedValue2); // Output: 0

const clampedValue3 = clamp(20, 0, 10);
console.log(clampedValue3); // Output: 10

Answer

function clamp(number,lower,upper){
	const num = Math.max(lower,number)
	return Math.min(upper,num)
}