Problem overview
The method allowToContest receives 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 inclusive.
In the first example, minAge = 7, maxAge = 12 and age = 7. The child is exactly at the lower limit, which counts as allowed, so the answer is true. In the second example, minAge = 3, maxAge = 8 and age = 10. Ten is above the upper limit, so the answer is false.
Approach: one condition, returned directly
An age is allowed when two things hold at once: it is at least minAge and it is at most maxAge. The "and" is the logical AND operator, &&, which is true only when both of its sides are true.
There are three ways to write this, and they all do the same work:
- Reject the bad cases first: return
falseifage < minAge, returnfalseifage > maxAge, and returntrueafter that. - Use one
ifwith both conditions joined by&&, returningtruein the branch andfalseafter it. - Skip the
ifentirely. A comparison such asage <= maxAgealready is aBoolean, and so is the result of&&, so the condition itself is the answer.
This lesson uses the third form. An if that returns true in one branch and false in the other only repeats the value of its own condition.
Step by step
The lesson has a single step, but it contains two decisions.
- Write the lower bound as
minAge <= age. It is true when the child is old enough, including the case where the age equalsminAge. - Write the upper bound as
age <= maxAge. It is true when the child is not too old, again including equality. - Join them with
&&and return the whole expression. ReadingminAge <= age && age <= maxAgefrom left to right gives the rangeminAge <= age <= maxAge.
Using < instead of <= is the usual slip. With <, the first example would return false for age 7, because 7 is not strictly greater than 7.
The solution in Scala
class Solution {
def allowToContest(minAge: Int, maxAge: Int, age: Int): Boolean = {
return minAge <= age && age <= maxAge
}
}
A few Scala details are worth knowing here:
- Chaining does not work. Scala has no chained comparison like
minAge <= age <= maxAge. The first comparison produces aBoolean, which then meetsmaxAge, anInt, and the compiler reports a type mismatch. Two comparisons joined by&&are required. - Precedence. The comparison operators bind tighter than
&&, so no parentheses are needed around either comparison. - Short-circuiting.
&&evaluates its right side only when the left side istrue. For plain integer comparisons this makes no visible difference, but it matters when the right side has a side effect or could fail. - The return keyword. The last expression of a method body is its result, so the
returnin the lesson's code is optional. Writing justminAge <= age && age <= maxAgeinside the braces returns the same value. Scala programmers usually leavereturnout. - The declared types. The signature promises a
Boolean, and the expression has that type, so no conversion is needed.
Edge cases and common mistakes
- The limits themselves.
allowToContest(3, 8, 8)returnstrue, andallowToContest(3, 8, 3)returnstruetoo. Both ends are inclusive. - Using
||instead of&&. With OR,minAge <= age || age <= maxAgeis true for nearly every age, because any age fails at most one of the two checks. The second example would wrongly returntrue. - Swapped limits. If
minAgeis larger thanmaxAge, no age can satisfy both conditions, so the method returnsfalsefor every age.allowToContest(8, 3, 5)givesfalse. - Redundant branches. Code such as
if (cond) true else falseworks but says the same thing ascondin more words.
Testing
minAge = 7,maxAge = 12,age = 7givestrue.minAge = 3,maxAge = 8,age = 10givesfalse.minAge = 3,maxAge = 8,age = 8givestrue.minAge = 3,maxAge = 8,age = 2givesfalse.
