I have kept at these CodingBat questions and I ran into another problem.
The assignment was this:
Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.
Some Examples:
- max1020(11, 19) → 19
- max1020(19, 11) → 19
- max1020(11, 9) → 11
My solution is:
public int max1020(int a, int b) {
if (((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) && a >= b) {
return a;
}
if (((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) && b >= a) {
return b;
} else {
return 0;
}
}
The code returned the correct output more than half of the time, but it did not work as expected in certain scenarios. The scenarios are when the input was (10,21),(21,10),(23,10).
This is weird because I explicitly excluded #>20 yet it returns 21, 21, and 23 respectively for the above three scenarios.
Where did I make a mistake?