Start learning free

LESSON 75 OF 348 · CONDITIONAL STATEMENTS

Is Number Negative in TypeScript: if and else

Practice Read the full explanation

Create a function that takes a number n as its only argument and prints "negative" to the console if n is less than zero, otherwise prints "positive" to the console.

Example 1:

Input: n = 5
Output: "positive"
Explanation:
5 is greater than or equal to zero, so our function prints "positive" to the console.

Example 2:

Input: n = 0
Output: "positive"
Explanation:
0 is greater than or equal to zero, so our function prints "positive" to the console.

Example 3:

Input: n = -2
Output: "negative"
Explanation:
-2 is less than zero, so our function prints "negative" to the console.

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    TYPESCRIPT · CONDITIONAL STATEMENTS

    Is Number Negative in TypeScript, explained

    Problem overview

    The method isNegative of the class Solution receives a number n. It doesn't return anything: it prints "negative" to the console when n is less than zero, and "positive" otherwise. Its return type is void, which says that the method's job is the printing.

    The three examples cover the cases. For n = 5 the method prints positive. For n = -2 it prints negative. The surprising one is n = 0: zero is not less than zero, so it also prints positive, even though zero is neither positive nor negative in mathematics. The task defines "positive" as "not negative".

    Approach

    This is a decision between two outcomes, which is exactly what an if-else statement does. One comparison is enough: ask whether n < 0. If the answer is yes, print one word. In every other case, print the other. There is no need for a second condition, because "otherwise" already covers zero and all positive numbers.

    Writing a second test such as n > 0 would be a trap: it would leave zero without any output, or force a third branch. A single < with an else splits all numbers into two groups with no gap.

    The branches could be written the other way round, with if (n >= 0) printing positive first. Both versions give the same output for every ordinary number. The lesson tests for the negative case first because that is the way the task is worded, and it keeps the condition a plain <.

    Step by step

    1. Write the if with its condition. Inside the method body, use if (n < 0) { }. The operator < is strict, so only values below zero get in.
    2. Print in the if block. Put console.log("negative"); inside, so the word appears only when the condition holds.
    3. Add the else. Attach else { } to the same statement and print "positive" there. It runs when n < 0 is false, which includes 0.

    The solution in TypeScript

    class Solution {
        isNegative(n: number): void {
            if (n < 0) {
                console.log("negative");
            } else {
                console.log("positive");
            }
        }
    }
    

    A few TypeScript details are worth noting:

    • The annotations are for the compiler. n: number and : void are checked while compiling and erased from the JavaScript that runs. A call such as isNegative("-3") gets a compiler error, but the JavaScript would still run, and the string would be converted to a number by <.
    • number covers integers and decimals. There is just one numeric type, so -0.5 works exactly like -2.
    • console.log does the output. It prints the text and moves to a new line. The method returns nothing, so there is no return statement.
    • It is a method on a class. To try it yourself you create an object first: const s = new Solution(); and then call s.isNegative(-2);.

    Edge cases and common mistakes

    • Zero. 0 prints positive. Using <= would print negative for zero.
    • Decimals. -0.5 prints negative and 0.5 prints positive.
    • Negative zero. JavaScript has a value -0, but -0 < 0 is false, so it prints positive, like zero.
    • NaN. Every comparison with NaN is false, so NaN < 0 is false and it prints positive. The tests don't try this, but it shows why the else branch is "everything else", not "greater than zero".
    • Returning instead of printing. Writing return "negative" would produce no output on the console. The task wants printing.
    • Wrong message text. The words are lowercase with no extra spaces or punctuation: "negative" and "positive".
    • Using = or a missing brace. if (n = 0) would assign. Unbalanced braces make the compiler report an error.

    Testing

    • n = 5 prints positive.
    • n = 0 prints positive.
    • n = -2 prints negative.
    • n = -0.5 prints negative.
    • n = 1000000 prints positive.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free