Start learning free

LESSON 126 OF 348 · LOOPS & CONDITIONALS

Print Even Numbers From an Array in TypeScript: Fixing 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 prints to the console the even numbers from that array.

So when we called printEvenNumbers([2, 1, 0, 4, 3]), we expected our code to print:

2
0
4

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

even
even

Assignment:

Your task is to fix our function such that it prints the even numbers from the array.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS & CONDITIONALS

    Buggy Code Print Even Numbers in TypeScript, explained

    What the code is supposed to do

    The function printEvenNumbers(nums) should print the even numbers of an array, one per line. For the call printEvenNumbers([2, 1, 0, 4, 3]) the expected output is:

    2
    0
    4
    

    The code you start with prints the word even twice instead:

    function printEvenNumbers(nums: number[]): void {
        for (const num of nums) {
            if (num % 2 === 1) {
                console.log("even");
            }
        }
    }
    
    // Do not change this code:
    printEvenNumbers([2, 1, 0, 4, 3]);
    

    There are two mistakes, and each one produces a different part of the wrong output.

    Mistake 1: the condition selects the odd numbers

    The operator % gives the remainder of a division. Dividing by 2, the remainder is 0 for an even number and 1 for a positive odd number. The condition num % 2 === 1 is therefore true for the odd values 1 and 3, and false for 2, 0 and 4.

    That is why the output has two lines: the array holds two odd numbers, and each one passes the test. The even numbers were never printed because they never passed it. The test should select a remainder of 0: num % 2 === 0.

    Mistake 2: the quotes print a word, not a value

    Text between quotes is a string, and console.log("even") prints exactly those four letters every time it runs, whatever num holds. To print the number, pass the variable itself, without quotes: console.log(num). The same line then prints a different value on each pass.

    The fixed code in TypeScript

    function printEvenNumbers(nums: number[]): void {
        for (const num of nums) {
            if (num % 2 === 0) {
                console.log(num);
            }
        }
    }
    
    printEvenNumbers([2, 1, 0, 4, 3]);
    

    Following the array through the loop shows why this works:

    • 2: remainder 0, so it is printed.
    • 1: remainder 1, so it is skipped.
    • 0: remainder 0, so it is printed. Zero is an even number.
    • 4: remainder 0, so it is printed.
    • 3: remainder 1, so it is skipped.

    The output is 2, 0 and 4, as required. The final call, below the function, is part of the lesson and stays as it is.

    Details worth knowing

    • === compares without converting. It is true only when both sides have the same type and value, so it is the safe choice for checking a remainder.
    • Negative numbers. In TypeScript, as in JavaScript, the remainder takes the sign of the left operand. So -3 % 2 is -1, not 1. The original test num % 2 === 1 would also miss negative odd numbers, and a fix that tests for 1 to find odd numbers would be wrong for them. Testing for a remainder of 0 works for every integer, negative ones included.
    • Negative even numbers. -8 % 2 gives -0, which equals 0 under ===, so the test num % 2 === 0 still passes for them.
    • Types do not catch this. Both the buggy and the fixed versions are valid TypeScript, since a string and a number can both be passed to console.log. The compiler checks that types fit together, not that the logic matches the task, so the bug shows up only in the output.

    Common mistakes

    • Fixing only one mistake. Changing the condition alone prints the word even three times. Changing the print alone prints the odd numbers 1 and 3.
    • Skipping zero. Writing num > 0 && num % 2 === 0 drops 0 from the output, but 0 is even and the expected output includes it.
    • Writing = instead of ===. A single = assigns a value rather than comparing, which is not what a condition needs.
    • Removing the last line. The call at the end makes the function run, and without it nothing is printed.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free