Start learning free

LESSON 79 OF 348 · CONDITIONAL STATEMENTS

Parity of Number in TypeScript: The Modulo Operator

Practice Read the full explanation

Given a non-negative integer n, check it's parity and return "even" or "odd"

A number is "even" if it is divisible by 2 and "odd" otherwise

Example:

Input: n = 12

Output: "even"

Explanation:
12 is divisible by 2, so it is even
Hints:

You can make use of modulo operator with an if statement

12 % 2 is 0, therefore 12 is "even"
13 % 2 is 1, therefore 13 is "odd"
STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · CONDITIONAL STATEMENTS

    Parity Of Number in TypeScript, explained

    Problem overview

    The method parity receives a non-negative integer n and returns the string "even" if n is divisible by 2 and "odd" otherwise. For n = 12 the answer is "even", because 12 divided by 2 leaves nothing over.

    Approach

    Divisibility is a question about remainders. The modulo operator % gives the remainder of a division: 12 % 2 is 0 and 13 % 2 is 1. A number is even exactly when its remainder after dividing by 2 is 0, and odd otherwise. Since the remainder by 2 can only be 0 or 1, there is nothing else to check.

    That gives a two-way choice: if the remainder is 0, return "even", and in every other case return "odd".

    Step by step

    1. Test for even. Write an if whose condition compares n % 2 with 0. The lesson uses ===, strict equality, which never converts types. Here both sides are numbers, so it behaves like ==, and === is the habit worth keeping.
    2. Return "even" inside the if. The method ends there for even numbers.
    3. Return "odd" after the if. This line is reached only when the condition was false, because an even number has already returned. That is why no else is needed.

    The solution in TypeScript

    class Solution {
        parity(n: number): string {
            if (n % 2 === 0) {
                return "even";
            }
            return "odd";
        }
    }
    

    Some TypeScript details about this code:

    • The test is against 0, not 1. n % 2 === 0 identifies even numbers, and everything else is odd. Testing for odd with n % 2 === 1 works for the non-negative inputs of this lesson, but it breaks for negative numbers. In JavaScript and TypeScript, the result of % takes the sign of the left operand, so -3 % 2 is -1, and a test against 1 would call -3 even.
    • The types. n: number and : string are annotations for the compiler. They are erased when the code is compiled to JavaScript, so they check nothing at run time. Calling the compiled method with a string would not be stopped by them.
    • Numbers can be fractional. TypeScript has a single number type, and the compiler accepts 7.5 where an integer is expected. For that input, 7.5 % 2 is 1.5, which is not 0, so the method returns "odd". The lesson only promises correct answers for integers.
    • Zero is even. 0 % 2 is 0, so parity(0) returns "even", which matches the mathematical definition.

    A shorter form

    The same logic fits in one expression with the conditional operator: return n % 2 === 0 ? "even" : "odd";. It returns the first string when the condition is true and the second when it is false. The lesson uses the if form because it shows each step separately, and the conditional operator is worth knowing once you are comfortable reading that form.

    To trace n = 13: 13 % 2 is 1, which is not 0, so the if block is skipped and the method reaches return "odd". To trace n = 12: 12 % 2 is 0, the condition is true, and the method returns "even" without ever reaching the last line.

    Complexity

    The method does one remainder operation and one comparison, whatever the size of n, so time is O(1). It stores nothing, so space is O(1).

    Edge cases and common mistakes

    • Zero. It is even, and the code handles it without a special case.
    • Using / instead of %. TypeScript's division does not discard the fraction: 13 / 2 is 6.5, never 6, so comparing a division with 0 does not test divisibility.
    • Using = instead of ===. A single = assigns, and n % 2 = 0 is a compile error because the left side cannot be assigned to.
    • Returning the wrong case or spelling. The strings must be exactly "even" and "odd", in lower case.
    • Testing for odd with === 1. This fails for negative inputs, as shown above, so prefer the test against 0.

    Testing

    const solution = new Solution();
    console.log(solution.parity(12));
    console.log(solution.parity(13));
    console.log(solution.parity(0));
    console.log(solution.parity(1));
    
    even
    odd
    even
    odd
    
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free