Start learning free

LESSON 131 OF 348 · LOOPS & CONDITIONALS

Positive Number in Array in TypeScript: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes an array nums as argument and returns true if there exists at least one positive (greater than zero) number in the array; returns false otherwise.

So when we called the function for [-1, 2, 3], we expected our code to print:

Array has positive numbers

but it seems like we made some mistakes because when we run our code, it prints:

Array doesn't have positive numbers

Assignment:

Your task is to fix our function.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS & CONDITIONALS

    Buggy Code Check For Positive Numbers in TypeScript, explained

    What the code is supposed to do

    The function checkForPositive receives an array of numbers and should return true if at least one of them is greater than zero, and false otherwise. The code below the function calls it with [-1, 2, 3] and prints a sentence based on the result. Since 2 and 3 are positive, the program should print Array has positive numbers.

    The code you start with prints the other sentence, Array doesn't have positive numbers:

    function checkForPositive(nums: number[]): boolean {
        for (const num of nums) {
            if (num > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    // Do not change this code:
    if (checkForPositive([-1, 2, 3])) {
        console.log("Array has positive numbers");
    } else {
        console.log("Array doesn't have positive numbers");
    }
    

    Your job is to find the mistake in checkForPositive and fix it.

    What goes wrong

    Follow the loop for [-1, 2, 3]. On the first pass, num is -1. The condition num > 0 is false, so the else branch runs, and it contains return false. A return ends the function on the spot. The loop never gets to 2 or 3, and the answer is false.

    The reasoning behind the bug is understandable: "this number is not positive, so the array has no positive number". But one number that is not positive tells you nothing about the others. The array has a positive number if any element is positive, so you can only say there is none after looking at every element.

    The function as written checks just the first element and answers for the whole array based on it. For an array that starts with a positive number it happens to work, which can make the bug hard to spot with a single test.

    A second consequence hides in the same code. For an empty array the loop body never runs, and the function ends without any return. A function that reaches its end without returning a value produces undefined, and the if in the calling code treats undefined as false. The output is the same sentence as for "no positive numbers", but only by accident.

    The shape of the fix

    Two different situations call for two different returns:

    • Finding a positive number settles the question immediately: the answer is true, and returning from inside the loop is right.
    • Not finding one can only be known once the loop has ended. So the false belongs after the loop, not inside it.

    This is the same pattern as any "does it exist?" search. A match returns early. The default answer waits at the end.

    Fixing it step by step

    1. Delete the else branch. Without it, a non-positive number simply lets the loop move on to the next element.
    2. Keep the return true. It stays inside the if so it runs as soon as a positive number turns up.
    3. Add return false after the loop. It runs only when every element has been checked and none was positive. It also covers the empty array.
    4. Leave the calling code alone. The task says not to change it.

    The fixed code in TypeScript

    function checkForPositive(nums: number[]): boolean {
        for (const num of nums) {
            if (num > 0) {
                return true;
            }
        }
        return false;
    }
    
    
    
    
    if (checkForPositive([-1, 2, 3])) {
        console.log("Array has positive numbers");
    } else {
        console.log("Array doesn't have positive numbers");
    }
    

    With the fix, the program prints:

    Array has positive numbers
    

    Some TypeScript details in this code:

    • The return type is a promise to the compiler only. The annotation : boolean says what the function should give back, and tsc checks the return statements against it. It does not change what happens when the program runs. In the lessons the buggy version compiles without complaint and returns undefined for an empty array.
    • A function can reach its end without returning. In JavaScript this produces undefined. The final return false removes that possibility, so the function always returns a real true or false.
    • Zero is not positive. The condition is num > 0, with a strict comparison, so [0] gives false.
    • The if accepts the returned boolean directly. The calling code writes if (checkForPositive(...)) without comparing the result to true.

    Common mistakes

    • Moving return false but keeping it in the loop. If it stays in the loop in any form, one element decides for the whole array.
    • Swapping to num < 0. The question is about positive numbers, so the condition must remain num > 0.
    • Using >=. With num >= 0, a zero would count as positive.
    • Counting instead of returning. Counting all the positive numbers and checking whether the count is above zero works, but it always visits the whole array, while the early return stops at the first match.
    • Editing the lines at the bottom. Changing the printed sentences or the argument may make the output look right while the function is still wrong.

    Testing

    • [-1, 2, 3] returns true, so the program prints Array has positive numbers.
    • [-1, -2] returns false.
    • [2, -1] returns true. The buggy version also gets this one right, which shows why it needs a test that starts with a non-positive number.
    • [] returns false.
    • [0] returns false.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free