Start learning free

LESSON 128 OF 348 · LOOPS & CONDITIONALS

Print Odd Numbers in TypeScript: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes a number n as argument and prints to the console the odd numbers from 0 through n.

So when we called printOddNumbers(8), we expected our code to print:

1
3
5
7

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


Assignment:

Your task is to fix our function such that it prints the odd numbers from 0 through n.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS & CONDITIONALS

    Buggy Code Print Odd Numbers in TypeScript, explained

    What the code is supposed to do

    The function printOddNumbers takes a number n and should print the odd numbers below it, one per line. Called with 8, it should print 1, 3, 5 and 7. The code you start with prints nothing at all:

    function printOddNumbers(n: number): void {
        for (let i: number = 0; i < n; i++) {
            if (n % 2 === 1) {
                console.log(n);
            }
        }
    }
    
    // Do not change this code:
    printOddNumbers(8);
    

    Your job is to find the mistake and fix it so that the right numbers are printed.

    What goes wrong

    The loop is fine. The variable i starts at 0, and the loop keeps going while i < n, so i takes the values 0, 1, 2, ..., 7. That is the range of numbers the function should look at.

    The trouble is inside the loop. Both lines use n where they should use i:

    • The condition n % 2 === 1 asks whether the parameter is odd. The parameter is 8 on every pass, and 8 is even, so the condition is false every single time.
    • The print line console.log(n) would print the parameter, not the number the loop is currently on.

    Since the condition never becomes true, the print line never runs, and nothing appears. The bug is easy to miss because the code reads naturally: "if the number is odd, print the number". The question is which variable holds the number being examined, and that is the loop variable, the one that changes on each pass.

    The bug has a second face. Call the original function with an odd argument such as 7, and the condition is true on every pass. The function then prints 7 seven times, which is just as wrong as printing nothing:

    7
    7
    7
    7
    7
    7
    7
    

    That output shows what the lines inside the loop are really doing: they never look at i.

    Fixing it step by step

    1. Leave the loop header alone. It already visits every value from 0 to n - 1.
    2. Fix the condition. Replace n with i, so the test becomes i % 2 === 1. The remainder of i divided by 2 is 1 exactly for the odd values.
    3. Fix the print. Replace n with i in console.log so the odd value itself is printed.
    4. Keep the call at the bottom. The line printOddNumbers(8); is what runs the function, and the task says not to change it.

    The fixed code in TypeScript

    function printOddNumbers(n: number): void {
        for (let i: number = 0; i < n; i++) {
            if (i % 2 === 1) {
                console.log(i);
            }
        }
    }
    
    
    printOddNumbers(8);
    

    Running it prints:

    1
    3
    5
    7
    

    Some TypeScript details about this code:

    • The type annotations do not catch this bug. Both n and i are of type number, so the compiler is equally happy with either one. A type checker cannot tell which variable you meant.
    • : void means nothing is returned. The function prints, and it has no return statement.
    • The explicit : number on i is optional. TypeScript infers it from the starting value 0. Annotations like this are erased when the code is compiled to JavaScript.
    • === compares without converting. For two numbers it behaves just like ==, and it is the comparison to prefer in TypeScript code.
    • The remainder operator keeps the sign. For the non-negative values in this loop, i % 2 is 0 or 1. For a negative number, such as -3 % 2, the result is -1, so a test against 1 would miss it.

    Common mistakes

    • Changing only one of the two lines. If you fix the condition but still print n, the function prints 8 four times. If you fix the print but not the condition, nothing is printed.
    • Changing the loop bound. Replacing i < n with i <= n adds the value n to the range. For 8 it changes nothing visible, because 8 is even, but for an odd argument it would print one extra line.
    • Printing the wrong numbers. Starting i at 1 and printing i + 1, or testing i % 2 === 0, prints the wrong set of values. The numbers printed must be odd, and they must come from the loop.
    • Touching the last line. The call printOddNumbers(8); must stay as it is.

    Checking your fix

    Run the code in the editor above after the change. With the argument 8, the output should be four lines: 1, 3, 5 and 7, with no other output. If you see nothing, an n is still where an i belongs. If you see the same number repeated, the print line is still using n.

    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free