Problem overview
The task is to return the largest of three integers, a, b and c, using two methods of the Solution class. The helper maxOfTwo takes two numbers and returns the larger one. The graded method maxOfThree must find the largest of the three by calling the helper, without comparing numbers itself and without a built-in max function.
For a = 3, b = 7, c = 5 the answer is 7. For -3, -8, -1 it is -1, the negative number closest to zero. When values tie, as in 4, 4, 2, the answer is that shared value, 4.
Approach: build the answer from a smaller one
Finding the larger of two numbers is easy. The largest of three is then the larger of two things: the larger of a and b, and c. If the winner of the first pair is beaten by c, c is the maximum; otherwise the winner of the pair is.
Splitting the work this way means the comparison is written once, in the helper, and reused. A function that solves a small piece of a problem is called a helper function, and the bigger function becomes one line.
Step by step
- Write the helper's first check. Inside
maxOfTwo, ifa > b, returna. Areturnends the function immediately, so nothing after it runs for that call. - Return
bbelow theif. The function only gets there whenais not greater thanb: eitherbis larger, or they are equal and either one is correct. Noelseis needed. - Call the helper twice in
maxOfThree.this.maxOfTwo(a, b)gives the larger of the first two, and that result is the first argument of an outer call withc. The inner call runs first.
The solution in TypeScript
class Solution {
maxOfTwo(a: number, b: number): number {
if (a > b) {
return a;
}
return b;
}
maxOfThree(a: number, b: number, c: number): number {
return this.maxOfTwo(this.maxOfTwo(a, b), c);
}
}
Details worth knowing:
this.is required. Inside a class, a method calls another method of the same object by writingthis.before its name. Writing justmaxOfTwo(a, b)looks for a plain function with that name: the compiler reportsCannot find nameand suggeststhis.maxOfTwo, and at run time the call fails with aReferenceError.- Nested calls. An argument can be any expression, including another call.
this.maxOfTwo(a, b)is evaluated to a number before the outer call receives it. - Types.
numberis TypeScript's single numeric type, used for both integers and decimals. The annotations are removed when the code is compiled to JavaScript and are not checked while the program runs. - Parameter names.
aandbinmaxOfTwoare separate variables from theaandbinmaxOfThree. Each call receives copies of the values it is given.
Edge cases and common mistakes
- Ties. With
a = 5andb = 5the conditiona > bis false, so the helper returnsb, which equalsa. The result is 5 either way. - All negative numbers. Starting from 0 as a "current maximum" would be wrong for
-3,-8,-1, as 0 would win. Comparing the numbers with each other has no such problem. - The maximum in any position. The answer can be
a,borc.maxOfThree(1, 2, 3)returns 3 andmaxOfThree(9, 2, 3)returns 9. - Comparing in
maxOfThree. Adding anifthere defeats the purpose of the lesson: the helper does all the comparing.
Testing
maxOfThree(3, 7, 5)gives7.maxOfThree(-3, -8, -1)gives-1.maxOfThree(4, 4, 2)gives4.maxOfThree(1, 2, 3)gives3.maxOfThree(9, 2, 3)gives9.
