Start learning free

LESSON 127 OF 348 · LOOPS & CONDITIONALS

Print Even Odd in TypeScript: a for Loop with if and else

Practice Read the full explanation

Given a positive integer n, for each number from 0 to n - 1, print "odd" if the number is odd or the number itself if it is even.

Example:

Input: n = 6
Output (console):
0
odd
2
odd
4
odd


Note:

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

STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS & CONDITIONALS

    Print Even Odd in TypeScript, explained

    Problem overview

    The method printEvenOdd takes a positive integer n and prints one line for each number from 0 up to n - 1. If the number is odd, the line is the word odd. If it is even, the line is the number itself. The method prints to the console and returns nothing, which is why its return type is void.

    For n = 6 the numbers are 0, 1, 2, 3, 4 and 5. Zero, 2 and 4 are even, so they appear as numbers. One, 3 and 5 are odd, so each becomes the word odd. The output is:

    0
    odd
    2
    odd
    4
    odd
    

    Notice that n itself is never printed: the range stops one short of it.

    Approach: visit every number and make one decision

    The task has two parts. First, visit every number in the range, which is a job for a for loop. Second, decide for each number which of two things to print, which is a job for if and else. Every number leads to exactly one print, so the two branches must not overlap and must not leave a gap. An if/else pair gives you both properties for free: the else branch runs precisely when the condition is false.

    The only real question is how to test for oddness. A number is odd when dividing it by 2 leaves a remainder of 1. TypeScript's remainder operator % returns that remainder, so i % 2 is 0 for even numbers and 1 for odd ones (for non-negative integers, which is all this lesson uses).

    Step by step

    1. Write the loop header. A loop that starts at 0 and stops before n uses let i = 0, the condition i < n, and i++ to move on after each pass. The strict < is what keeps n out of the range. Using <= would print one line too many.
    2. Test for odd inside the loop. The body starts with an if whose condition compares i % 2 with 1. When it is true, the body of the if prints the string "odd" with console.log.
    3. Handle the even numbers in the else branch. Any number that failed the odd test is even, so print i itself. Because console.log(i) receives a number, it prints the digits without quotes.

    Order does not matter much here. You could also test for even first and print "odd" in the else branch. The lesson tests for odd first because that matches the order in which the task describes the two cases.

    The solution in TypeScript

    class Solution {
        printEvenOdd(n: number): void {
            for (let i = 0; i < n; i++) {
                if (i % 2 == 1) {
                    console.log("odd");
                } else {
                    console.log(i);
                }
            }
        }
    }
    

    A few TypeScript details are worth knowing here:

    • The annotations are only for the compiler. n: number and : void tell tsc what to expect, and they are erased when the code becomes JavaScript. Nothing checks at run time that n really is a number.
    • There is one numeric type. Whole numbers and decimals are both number, so the loop variable i is inferred as number without being annotated.
    • == versus ===. The solution compares with ==. When both sides are numbers, it gives the same answer as ===, the strict comparison. The strict form is the habit to build, because == converts types when the two sides differ and can surprise you.
    • A string and a number print alike. console.log("odd") and console.log(7) both show their value without quotes, so the output is a clean column of lines.
    • Remainders keep the sign of the left side. -3 % 2 is -1, not 1. That cannot matter here because the loop only produces non-negative numbers, but it is the reason a test like i % 2 === 1 is only safe for non-negative input. Testing i % 2 === 0 for even and treating everything else as odd works for negatives too.
    • / is not integer division. 7 / 2 is 3.5. For this lesson that does not come up, because % alone answers the question.

    Edge cases and common mistakes

    • Off-by-one on the range. The range runs from 0 to n - 1. Starting at 1 drops the leading 0 line, and using i <= n adds a line for n.
    • Zero counts as even. Zero divided by 2 leaves remainder 0, so the first line is the number 0, not the word odd.
    • Very small input. With n = 1 the loop runs once and prints 0. With n = 0 it never runs and prints nothing.
    • Returning instead of printing. The method has to print each line as it goes. Building one string or an array and returning it is a different task, and the lesson's tests look at what is printed.
    • Using = in the condition. A single = assigns a value instead of comparing, so if (i % 2 = 1) does not compile.
    • Quotes around i. Writing console.log("i") prints the letter i on every even pass instead of the number.
    • Two separate if statements. An if for odd followed by another if for even also works, but it tests twice. The else says it better: whatever was not odd is even.

    Testing

    • n = 6 prints 0, odd, 2, odd, 4, odd, one per line.
    • n = 1 prints just 0.
    • n = 0 prints nothing.
    • n = 3 prints 0, odd, 2.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free