Start learning free

LESSON 132 OF 348 · LOOPS & CONDITIONALS

Hurdle Jump in TypeScript: Checking Every Hurdle with a Loop

Practice Read the full explanation

Given an array of hurdle heights and a jumper's jump height, determine whether or not the hurdler can clear all the hurdles. If they can, return true; otherwise return false.

A hurdler can clear a hurdle if their jump height is greater than or equal to the hurdle height.

Examples:

hurdleJump([1, 2, 3, 4, 5], 5) ➞ true

hurdleJump([5, 5, 3, 4, 5], 3) ➞ false

hurdleJump([5, 4, 5, 6], 10) ➞ true

hurdleJump([1, 2, 1], 1) ➞ false

Note: Return true for the edge case of an empty array of hurdles. (Zero hurdles means that any jump height can clear them).

STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS & CONDITIONALS

    Hurdle Jump in TypeScript, explained

    Problem overview

    The method canJump receives an array of hurdle heights, hurdleHeights, and the height the jumper can clear, jumperHeight. It returns true if the jumper can clear every hurdle and false if at least one hurdle is too tall. A hurdle is cleared when the jump height is greater than or equal to the hurdle height, so a hurdle exactly as tall as the jump still counts as cleared.

    Take hurdles [1, 2, 3, 4, 5] and a jump of 5. The tallest hurdle is 5, which is equal to the jump, so every hurdle is cleared and the answer is true. With hurdles [5, 5, 3, 4, 5] and a jump of 3, the first hurdle is already too tall, so the answer is false. With [1, 2, 1] and a jump of 1, the hurdle of height 2 is too tall, so the answer is false even though the other two are fine.

    An empty array of hurdles must give true: with no hurdles in the way, any jump height gets through.

    Approach: one bad hurdle is enough

    The question is "can the jumper clear all of them?". That phrasing leads to a pattern. A single hurdle that is too tall settles the answer: it is false, and there is no point in looking further. On the other hand, a hurdle that is low enough proves nothing about the rest. The answer can only be true after every hurdle has passed the test.

    So the method visits the hurdles one by one. If any of them is taller than the jumper can clear, it returns false immediately. If the loop ends without that happening, every hurdle was low enough, and the method returns true.

    It is easier to test the failing case than the passing one. "Is this hurdle too tall?" is one strict comparison, while the passing case would be the opposite, and you would have to combine the results of all the hurdles. Stopping at the first failure avoids that.

    Step by step

    1. Loop over the heights. The for...of loop gives the elements of hurdleHeights directly. The loop variable is called height, and each pass holds the height of one hurdle.
    2. Return false on a hurdle that is too tall. The test is height > jumperHeight. It is a strict greater-than because equal heights are allowed: the jumper clears a hurdle of height 5 with a jump of 5. Using >= would wrongly reject that case. The return ends the method at once, so the remaining hurdles are skipped.
    3. Return true after the loop. The line is reached only if no hurdle triggered the early return. That means every hurdle was cleared. For an empty array the loop does not run at all, so the method returns true, exactly as the problem demands, with no special case written.

    The solution in TypeScript

    class Solution {
        canJump(hurdleHeights: number[], jumperHeight: number): boolean {
            for (const height of hurdleHeights) {
                if (height > jumperHeight) {
                    return false;
                }
            }
            return true;
        }}
    

    A few details on this code:

    • The method is called canJump. The examples in the task call it hurdleJump, but the editor and the lesson's tests use canJump inside the class Solution, so that is the name to keep.
    • for...of versus for...in. for (const height of hurdleHeights) gives numbers. for...in gives the indices as strings, and comparing a string index with a number would silently check the wrong thing.
    • Closing braces on one line. The reference puts the class's final } right after the method's, as }}. It is only a formatting choice and does not change the meaning.
    • The annotations disappear at run time. number[] and boolean are checked by the compiler and erased from the JavaScript. The loop itself only relies on the comparison.
    • Comparisons with NaN are always false. A hurdle height of NaN would never be greater than the jump, so it would never fail the check. That is not something the lesson's inputs contain.

    Complexity

    Time: O(n), where n is the number of hurdles. In the worst case, every hurdle is cleared and the loop looks at all of them. If an early hurdle is too tall, the method stops sooner.

    Space: O(1). Only the loop variable is used, and no copy of the array is made.

    Edge cases and common mistakes

    • Empty array. The result is true, because nothing blocks the jumper.
    • Equal height. A hurdle of 3 with a jump of 3 is cleared. Writing >= in the test gives the wrong answer here.
    • Returning true inside the loop. If you return true for the first hurdle that is low enough, the method would give true for [1, 9] with a jump of 5, ignoring the 9.
    • Checking only the first or last hurdle. The tallest hurdle can be anywhere in the array, so every element needs a look.
    • Comparing in the wrong direction. The failing condition is the hurdle being taller than the jump, height > jumperHeight, not the other way around.
    • Forgetting the final return true. The method would give back undefined whenever the loop finishes, including for the empty array.

    Testing

    • [1, 2, 3, 4, 5] with a jump of 5 gives true.
    • [5, 5, 3, 4, 5] with a jump of 3 gives false.
    • [5, 4, 5, 6] with a jump of 10 gives true.
    • [1, 2, 1] with a jump of 1 gives false.
    • [] with any jump gives true.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free