Problem overview
The function allowToContest takes three integers: minAge, maxAge and age. It returns true when the child's age lies between the two limits, and false otherwise. Both limits are allowed values.
In the first example the limits are 7 and 12 and the child is 7. That is exactly the lower limit, so the answer is true. In the second example the limits are 3 and 8 and the child is 10, which is above the upper limit, so the answer is false.
Approach
An age is acceptable when two things hold at once: it is at least minAge, and it is at most maxAge. There are three ways to write that, all correct:
- Check for failures. Return
falseifage < minAge, returnfalseifage > maxAge, and otherwise returntrue. - Use one
ifwith both conditions joined by&&, returningtrueorfalsefrom its branches. - Skip the
if. The joined condition is already abool, so return it directly.
The lesson takes the third route. Its idea is the same one as in the previous lesson on boolean expressions: a function that answers a yes-or-no question can return the condition itself.
Step by step
- Write the lower-bound test.
minAge <= ageis true when the child is old enough. Both ends are inclusive, so the operator is<=, not<. - Write the upper-bound test.
age <= maxAgeis true when the child is not too old. - Join them with
&&, which is true only when both sides are true, and return the result.
Writing the two tests so the age sits between the limits, minAge <= age && age <= maxAge, mirrors the mathematical range minAge <= age <= maxAge and makes the intent easy to see.
The solution in Go
func allowToContest(minAge int, maxAge int, age int) bool {
return minAge <= age && age <= maxAge
}
Go details worth knowing:
- No chained comparison. Go can't evaluate
minAge <= age <= maxAge. It would compare a boolean with a number, and the compiler refuses. Two comparisons joined by&&are required. - Precedence. Comparison operators bind tighter than
&&, so each side is evaluated first and then combined, with no parentheses. - Short-circuiting. If
minAge <= ageis false, Go does not evaluate the second comparison, because the result is alreadyfalse. Here this only saves a tiny amount of work. - Parameter grouping. The signature can also be written
(minAge, maxAge, age int), which means the same thing. The lesson repeatsintfor each parameter.
Edge cases and common mistakes
- The boundaries. Ages equal to
minAgeormaxAgeare allowed. Using<would reject them. - Using
||instead of&&. Every age is either at least the minimum or at most the maximum, so the function would almost always returntrue. - One-value range. If
minAgeequalsmaxAge, only that exact age passes. - Reversed limits. If
minAgeis greater thanmaxAge, no age can satisfy both tests, so the function always returnsfalse. The lesson doesn't ask you to handle this. - Negative or zero ages. The function does no validation. It compares the three numbers as given, so
allowToContest(0, 5, 0)istrueand a negative age is simply below any non-negative minimum. - Mixing up the parameter order. The arguments come as
minAge, maxAge, age. Swapping the last two would test the wrong value.
Testing
allowToContest(7, 12, 7)returnstrue.allowToContest(3, 8, 10)returnsfalse.allowToContest(7, 12, 12)returnstrue, the upper boundary.allowToContest(7, 12, 6)returnsfalse.allowToContest(5, 5, 5)returnstrue.allowToContest(9, 4, 6)returnsfalse.