Problem overview
Given three integers a, b and c, return the largest. The twist is the structure: maxOfTwo(a, b) is a helper that returns the larger of two numbers, and maxOfThree(a, b, c) must get its answer by calling the helper. It makes no comparison of its own, and no built-in max function is allowed.
For 3, 7 and 5 the answer is 7. For -3, -8 and -1 it is -1, because -1 is closest to zero. For 4, 4 and 2 it is 4: when two numbers tie for the largest, either one is the answer, since the value is the same.
Approach
The largest of three numbers is the larger of two things: the winner among the first two, and the third number. If maxOfTwo can pick a winner between two numbers, calling it twice picks the winner among three. The inner call finds the larger of a and b, and the outer call compares that result with c.
This is the point of a helper function: write the small piece of logic once, test it on its own, then build bigger answers out of it.
Step by step
- Return
awhen it is greater. InsidemaxOfTwo,if (a > b)returnsa. Areturnends the function immediately, so nothing below it runs in that case. - Return
botherwise. The line after theifonly runs whenais not greater thanb. Eitherbis larger or the two are equal, and in both casesbis a correct answer. Noelseis needed. - Nest two calls in
maxOfThree.this.maxOfTwo(a, b)runs first, and its result becomes the first argument of the outer call, whose second argument isc.
The solution in JavaScript
class Solution {
maxOfTwo(a, b) {
if (a > b) {
return a;
}
return b;
}
maxOfThree(a, b, c) {
return this.maxOfTwo(this.maxOfTwo(a, b), c);
}
}
Details that matter in JavaScript:
- Methods call each other through
this. Inside a class, writingmaxOfTwo(a, b)alone looks for a variable or function with that name, not for the method, and fails with aReferenceError.this.maxOfTwo(a, b)finds the method on the same object. - Parameter names are local. Both functions have parameters named
aandb. Each function has its own copies, so the names don't clash. InmaxOfThree, the outer call receives the inner call's result as its owna. - Arguments are evaluated first. The inner call finishes before the outer call starts, which is why nesting works.
Edge cases and common mistakes
- Negative numbers. Starting from a hard-coded 0 as "the maximum so far" would break
maxOfThree(-3, -8, -1), which must return -1. Comparing only the given numbers avoids this. - Ties.
maxOfTwo(4, 4)returns 4 through the secondreturn. Writinga >= bwould also be correct. - Wrong position of the maximum. The largest number can be first, middle or last.
maxOfThree(9, 1, 2)andmaxOfThree(1, 2, 9)both return 9, because every number goes through at least one comparison. - Forgetting
this.. It is the most common error in this lesson, and it fails with aReferenceErrorinstead of a wrong answer.
Testing
maxOfTwo(3, 7)gives7, andmaxOfTwo(4, 4)gives4.maxOfThree(3, 7, 5)gives7.maxOfThree(-3, -8, -1)gives-1.maxOfThree(4, 4, 2)gives4.maxOfThree(9, 1, 2)andmaxOfThree(1, 2, 9)both give9.
