Start learning free

LESSON 239 OF 671 · HELPER FUNCTIONS

Max of Three with a Helper in JavaScript

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

    JAVASCRIPT · HELPER FUNCTIONS

    Max Of Three Helper in JavaScript, explained

    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

    1. Return a when it is greater. Inside maxOfTwo, if (a > b) returns a. A return ends the function immediately, so nothing below it runs in that case.
    2. Return b otherwise. The line after the if only runs when a is not greater than b. Either b is larger or the two are equal, and in both cases b is a correct answer. No else is needed.
    3. 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 is c.

    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, writing maxOfTwo(a, b) alone looks for a variable or function with that name, not for the method, and fails with a ReferenceError. this.maxOfTwo(a, b) finds the method on the same object.
    • Parameter names are local. Both functions have parameters named a and b. Each function has its own copies, so the names don't clash. In maxOfThree, the outer call receives the inner call's result as its own a.
    • 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 second return. Writing a >= b would also be correct.
    • Wrong position of the maximum. The largest number can be first, middle or last. maxOfThree(9, 1, 2) and maxOfThree(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 a ReferenceError instead of a wrong answer.

    Testing

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