Start learning free

LESSON 125 OF 348 · LOOPS & CONDITIONALS

Print Positive Numbers From an Array in TypeScript

Practice Read the full explanation

Given an array of integers, print to the console the positive numbers, one on each line.

A number n is positive if it is strictly greater than 0.

Example:

Input: nums = [3, -2, 0, 4, -4]
Output (console):
3
4

Note:

You have to print the numbers to the console, your function shouldn't return anything.


STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS & CONDITIONALS

    Print Positive Numbers From Array in TypeScript, explained

    Problem overview

    The method printPositiveNumbers(nums) receives an array of numbers. It must print every positive number to the console, one per line, in the order they appear in the array. A number is positive if it is strictly greater than 0, so zero is not printed. The method returns nothing: its job is the printing.

    For nums = [3, -2, 0, 4, -4] the output is:

    3
    4
    

    The values -2 and -4 are negative, and 0 is neither positive nor negative, so only 3 and 4 are printed.

    Approach: visit each element and test it

    The task is a filter: look at every element once, and act on it only if it passes a test. That takes two ingredients, a loop that visits each element of the array and an if statement that decides whether to print. No bookkeeping is needed, because nothing has to be remembered from one element to the next.

    The array is printed in its original order simply because the loop visits the elements in that order.

    Step by step

    1. Loop over the array with for...of. This loop hands you one element at a time, so you do not manage an index. The loop variable is named val, and it is declared with const.
    2. Test the value. Inside the loop, an if checks val > 0. The comparison is strict, which keeps zero out.
    3. Print the value. Inside the if, console.log(val) prints it on its own line.

    The solution in TypeScript

    class Solution {
        printPositiveNumbers(nums: number[]): void {
            for (const val of nums) {
                if (val > 0) {
                    console.log(val);
                }
            }
        }
    }
    

    The TypeScript details worth knowing:

    • nums: number[]. This type says that the argument is an array of numbers. It is checked by the compiler only. When the code runs, the annotation is gone, so the type does not stop a caller from passing something else if the compiler has been bypassed.
    • for...of gives values, not positions. for (const val of nums) runs once per element and sets val to that element. Do not confuse it with for...in, which gives the indices, as strings.
    • const in the loop header. Each pass creates a fresh val, so const is allowed. The compiler reports an error if the loop body tries to assign to it. Use const when the variable is only read.
    • void. The return type says that no value comes back. The output happens through console.log.
    • Printing numbers. console.log(val) prints a number in its usual decimal form, without quotes. The lines match the expected output: 3 and 4.

    Complexity

    Time: O(n), where n is the length of the array. Each element is visited once, with a constant amount of work per element.

    Space: O(1). The method keeps no extra collection. It prints values as it finds them.

    Edge cases and common mistakes

    • Zero. Writing val >= 0 would print 0 for the example. The task says strictly greater than 0.
    • An empty array. The loop body never runs, so nothing is printed, and nothing goes wrong.
    • No positive values. For [-1, -5, 0] the method prints nothing at all.
    • Returning instead of printing. The method is declared void. Building and returning an array of positives is a different task, and it would print nothing.
    • Printing the wrong thing. console.log("val"), with quotes, would print the word val for every positive number. Without quotes, it prints the number.
    • Duplicates. Repeated values are printed as many times as they occur: [2, 2] prints 2 twice.
    • Stopping early. A break or return after the first match would skip the rest of the array. The loop has to visit every element.

    Testing

    • [3, -2, 0, 4, -4] prints 3 and 4.
    • [] prints nothing.
    • [-1, -5, 0] prints nothing.
    • [5, 5, -5] prints 5 twice.
    • [1, 2, 3] prints 1, 2 and 3.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free