Start learning free

LESSON 242 OF 289 · HELPER FUNCTIONS

Max of Three with a Helper Function in TypeScript

Practice Read the full explanation

Given three integers a, b and c, return the largest of them.

This time the work is split between two functions. The first one is a helper: it gets two numbers and returns the larger one. The second one, the function that is graded, finds the largest of the three numbers by calling the helper, so it never compares numbers itself.

Don't use a built-in max function: the helper does the comparing. The numbers can be negative, and some of them can be equal.

 

Example 1:

Input: a = 3, b = 7, c = 5
Output: 7
Explanation: The larger of 3 and 7 is 7, and the larger of 7 and 5 is still 7.

Example 2:

Input: a = -3, b = -8, c = -1
Output: -1
Explanation: All three numbers are negative. -1 is the closest to zero, so it is the largest.

Example 3:

Input: a = 4, b = 4, c = 2
Output: 4
Explanation: a and b tie for the largest. Either one is the answer, because the value is the same: 4.

 

Constraints:

  • a, b and c are integers between -1000000 and 1000000.
  • Two or all three of them can be equal.
STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · HELPER FUNCTIONS

    Max Of Three Helper in TypeScript, explained

    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

    1. Write the helper's first check. Inside maxOfTwo, if a > b, return a. A return ends the function immediately, so nothing after it runs for that call.
    2. Return b below the if. The function only gets there when a is not greater than b: either b is larger, or they are equal and either one is correct. No else is needed.
    3. 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 with c. 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 writing this. before its name. Writing just maxOfTwo(a, b) looks for a plain function with that name: the compiler reports Cannot find name and suggests this.maxOfTwo, and at run time the call fails with a ReferenceError.
    • 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. number is 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. a and b in maxOfTwo are separate variables from the a and b in maxOfThree. Each call receives copies of the values it is given.

    Edge cases and common mistakes

    • Ties. With a = 5 and b = 5 the condition a > b is false, so the helper returns b, which equals a. 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, b or c. maxOfThree(1, 2, 3) returns 3 and maxOfThree(9, 2, 3) returns 9.
    • Comparing in maxOfThree. Adding an if there defeats the purpose of the lesson: the helper does all the comparing.

    Testing

    • maxOfThree(3, 7, 5) gives 7.
    • maxOfThree(-3, -8, -1) gives -1.
    • maxOfThree(4, 4, 2) gives 4.
    • maxOfThree(1, 2, 3) gives 3.
    • maxOfThree(9, 2, 3) gives 9.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free