Kotlinlearncs.online LogoJava
Return to List

Solve: clamp

Created By: Justin Maier
/ Version: 2024.8.0

A "clamp" function is a value that takes three inputs: a lower bound, an upper bound, and the value that will be clamped. If this value is between the lower and upper bound, the original value is simply returned. If it is greater than the upper bound, the upper bound should be returned. Similarly, if it is less than the lower bound, the lower bound should be returned.

Write a function clamp that takes three int arguments: the value to be clamped, a lower bound, and an upper bound, in this order. It should return the "clamped" value of the first argument. You can assume that the lower bound will never be greater than the upper bound.

For example, clamp(6, 5, 10) should return 6 while clamp(11, 5, 10) should return 10.

Related Lessons

Stuck? You may find these lessons helpful: